简体   繁体   English

Javascript,两个async / await和promise func,但是一个正在工作,另一个不在工作

[英]Javascript, two async/await and promise func but one is working and the other is not working

my utils.js have two funcs. 我的utils.js有两个功能。 i using in the electron@2.0.2. 我在电子@ 2.0.2中使用。

async function detectMimeType(filePath) {
    let detectMime=new Promise((resolve, reject) => {
          // mmmagic npm module
          magic.detectFile(filePath, function(err, result) {
               if (err) reject(err);
               resolve(result);
          });
    });

    let result = await detectMime;

    return result;
}

async function parseXML(data) {
    let parse = new Promise((resolve, reject) => {
        // xml2js npm module
        parser.parseString(data,function(err,xmldata){
            if (err) reject(err);
            resolve(xmldata);
        });
    });

    let result = await parse;

    return result;
}

detectMimeType func returning result. detectMimeType函数的返回结果。 but parseXML func returning Promise object pending. 但是parseXML func返回Promise对象未决。

let mime = detectMimeType('testingSound.mp3'); // returning 'audio/mpeg';
let xmlPayload = 
    parseXML('<root><test>testData</test></root>'); //returning Promise object pending

but it works like this. 但是它是这样的。

async function init(){
    let xmlPayload = 
    await parseXML('<root><test>testData</test></root>');
}

Why does not it work like detectMimeType? 为什么它不能像detectMimeType一样工作?

UPDATE: detectMimeType and parseXML two func returning Promise Object. 更新: detectMimeType和parseXML两个函数返回Promise对象。 async functions always return a Promise. 异步函数总是返回Promise。

Functions declared as async function always return promises, even if their body is completely synchronous: 被声明为async function总是返回promise,即使它们的主体是完全同步的:

async function foo() {
    // seemingly synchronous code
    return "bar!";
}

const a = foo(); // Promise<pending>
const b = await foo(); // "bar!"

Whatever you return from the async function, is passed to the either resolve() or reject() function of the implicit promise that has been created as the result of async function invocation. 无论您从async函数返回的结果如何,都将传递给作为async function调用结果而创建的隐式promise的resolve()reject()函数。

So, you have always await foo() for the result, or use foo().then() if you are forced to use callbacks. 因此,您始终要await foo()作为结果,或者如果您被迫使用回调foo().then() ,请使用foo().then()

In general case, return await promise is merely a self-documentating construct, effectively equivalent to return promise , because promise would be in any case awaited as part of the implicit return promise resolve process. 在一般情况下, return await promise只是一个自我说明的构造,实际上等效于return promise ,因为在任何情况下,都会作为隐式退货保证解决过程的一部分来等待诺言。

Also, given your intent, I'd suggest promisifying your functions using standard Node.js util.promisify or Bluebird.promisify (whichever is available to you): 另外,鉴于您的意图,我建议您使用标准Node.js util.promisifyBluebird.promisify (以您可以使用的为准)来使您的功能util.promisify

const { promisify } = require('util');

const detectMimeType = promisify(magic.detectFile);
const parseXML = promisify(parser.parseString);

This keeps them functionally equivalent to the code you have produced already, but restraints from excessive use of new Promise() , generally frowned upon. 这样可以使它们在功能上等同于您已经生成的代码,但是可以避免过度使用通常不赞成使用的new Promise()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM