节点js,FS,回调异步

时间:2017-07-25 13:36:11

标签: javascript node.js asynchronous

我有一个代码,我在其中创建一个新目录。但问题是回调会在创建目录时更早调用。这个方法在类中。如何等待文件创建然后调用回调?我需要async / await,但我不知道如何包装它。任何解决方案?

 createMissingPath(newFilePathname, callback) {
    try {
        let currentIndex = 0;
        while ((currentIndex = newFilePathname.indexOf('/'/*path.sep*/, currentIndex+1))!= -1) {
            let lastPath = newFilePathname.substr(0, currentIndex);
            if(!fs.existsSync(lastPath)) {
                logger.info('Trying to create directory: %s', lastPath);
                fs.mkdir(lastPath);
            }
        }
        callback(null, null);
    }
    catch (exception) {
        logger.error('Caught exception : %s', inspect(exception));
        callback(exception, null);
    }
}

0 个答案:

没有答案
相关问题