简体   繁体   English

Javascript 异步 function 而不是新的 promise

[英]Javascript async function instead of new promise

I just learnt " async " that " async " ensures that the function returns promise then i tried to make a promise with writing " async " instead of writing " new Promise " and it didn't work out so i want to know is it wrong syntax or it will never work out and i will have to write only new promise ?我刚刚学会了“ async ”,“ async ”确保 function 返回 promise 然后我尝试用写“ async ”而不是写“ new Promise ”来制作 promise 但它没有成功所以我想知道它是不是错了语法或它永远不会成功,我将不得不只写新的 promise


// This is a code // 这是一段代码

async function gets(){
    let gets1 = async()=>{
        return 45;
    }
    gets1.then((value)=>{
        console.log(value, "inside gets1")
    })
}
gets()

As already pointed out, you need to call a function fn().then( , not just reference it fn.then( .正如已经指出的那样,您需要调用function fn().then( ,而不仅仅是引用它fn.then(
Also if you don't use the await , you also don't need the wrapping await另外,如果你不使用await ,你也不需要包装await

 const gets = () => { const gets1 = async() => { return 45; } gets1().then((value) => { console.log(value, "inside gets1") }) }; gets();

Or use async with await to get rid of Promise.then() :或者使用async with await来摆脱Promise.then()

 const gets = async () => { const gets1 = async() => { return 45; }; const value = await gets1(); console.log(value, "inside gets1") }; gets();

Make sure to read top to bottom: async Function确保从上到下阅读: async Function

You forgot to call the function您忘记拨打 function

It should be gets1().then() and not gets1.then()它应该是gets1().then()而不是gets1.then()

 async function gets(){ let gets1 = async()=>{ return 45; } gets1().then((value)=>{ console.log(value, "inside gets1") }) } gets()

The whole thing will work without the outer async function too:整个事情也可以在没有外部async function 的情况下工作:

 let gets1 = async()=>"msg from gets1"; gets1().then(console.log)

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

相关问题 无论如何,在第一个 function 中用异步重写这个 function。而不是“新的承诺”? - Is there anyway to rewrite this function with async in the first function. Instead of a "new Promise"? Javascript 反模式 - 从异步 function 返回一个新的 Promise - Javascript Anti-Pattern - Returning a new Promise from async function 通过 map() 向 object 添加新的键/值 - add new key/value to object via map() function - async promise returns a pending promise instead of the Value JavaScript,异步等待返回 promise 而不是结果 - JavaScript, async await is returning a promise instead of the result 异步 function 返回 promise,而不是值 - Async function returning promise, instead of value 异步等待函数返回承诺而不是字符串 - async await function returns promise instead of string 角异步函数返回undefined而不是promise - Angular async function returns undefined instead of promise javascript:异步 function 不返回从 ZDB974238714CA8DE634A7CE1D083A4 获取的 object 而是返回 [Fobject promise] - javascript: async function not returning fetched object from API but instead returns [object promise] 异步功能与返回新承诺 - Async function versus return New Promise 在JavaScript中的异步函数中捕获所有承诺拒绝 - Catching All Promise Rejections in an Async Function in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM