如何在 Javascript 中使用异步等待函数对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34210812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:23:16  来源:igfitidea点击:

How to use async await function object in Javascript?

javascriptasync-awaitbabeljsecmascript-next

提问by bozzmob

Say I have a function object-

假设我有一个函数对象-

setObj : function(a,b){
    obj.a = a;
    obj.b = b;
}

If I have to use async & await on this function object, how do I do it?

如果我必须在这个函数对象上使用 async & await,我该怎么做?

If the same was written in function (function way), say-

如果同样写在函数(函数方式)中,说-

async function setObj(a,b){
    obj.a = a;
    obj.b = b;
}

await setObj(2,3);

This works fine. But, how do I do it in case of function object?

这工作正常。但是,如果是函数对象,我该怎么做?

回答by Ben March

If I understand your question correctly, you can just use the asynckeyword in front of the method declaration:

如果我正确理解您的问题,您可以async在方法声明前使用关键字:

let obj = {};
let myObj = {
    async setObj(a,b) {
        obj.a = a;
        obj.b = b;
    }
}

See http://tc39.github.io/ecmascript-asyncawait/#async-methods

http://tc39.github.io/ecmascript-asyncawait/#async-methods

UPDATE

更新

You cannot use awaitoutside of an async function. In order to use this you have to wrap that call to await setObj(2, 3):

您不能await在异步函数之外使用。为了使用它,您必须将该调用包装为await setObj(2, 3)

async function consoleLog() {
    await myObj.setObj(2, 3);
    console.log(obj.a + obj.b);
}

consoleLog();

回答by Hans

Use the same asynckeyword in your object's property:

async在对象的属性中使用相同的关键字:

(async function () {
  var obj = {};
  console.log("hello");

  let setObj = async function (a,b){
    obj.a = a;
    obj.b = b;
  };

  await setObj(2,3);

  console.log(obj.a+obj.b);
})();

Note that the entire code is wrapped in an asynchronous self-invoking function. This is needed, otherwise the await setObjwill not be able to run correctly.

请注意,整个代码都包含在一个异步自调用函数中。这是必需的,否则await setObj将无法正确运行。

回答by Willem van der Veen

You can simply put the asynckeyword on any function, not only function declarations but also function expressions and methods of object. For example:

您可以简单地将async关键字放在任何函数上,不仅是函数声明,还包括函数表达式和对象方法。例如:

As an method of an object:

作为对象的方法:

const Object = {
    async asyncFunction() {
        await MyExamplepromise
    }
}

As a variable:

作为变量:

const myFunc = async function () { await MyExamplepromise }

// this is how execute the function expression
// first () operator to execute, and use .then get access the resolved value
myFunc().then((val) => { console.log(val) })


Also notice that an async function returns a promise which will be resolved with the value returned by the async function, or rejected with an uncaught exception thrown from within the async function.

另请注意,异步函数返回一个承诺,该承诺将使用异步函数返回的值进行解析,或者被异步函数内抛出的未捕获异常拒绝。

回答by Prasanna

using arrow functions work as well

使用箭头函数也可以

const myObject = {
   myFunc: async () => {
     await myResultHere
   }
}

using this: Since the function is an async function, it will run asynchronously. If you want to run this with await, you will have to use it inside an async function

使用这个:由于该函数是一个异步函数,它将异步运行。如果你想用 await 运行它,你必须在异步函数中使用它

const useFunc = async () => {
   const res = await myObject.myfunc();
}