Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

2
  • 2
    Doesn't work. This: await setTimeout(()=>{console.log('first')}, 200); console.log ('second') prints second then first Commented Aug 30, 2019 at 20:52
  • 2
    @gregn3 that is the point yes. This is a non blocking solution where code outside the function can continue to execute while a "blocking operation" is completed outside the main program flow. Although the syntax you and Rommy and Mohamad have provided isn't strictly correct due to the requirement for an await to be rapped in an async function (might be a fairly recent addition), also I'm using node.js. This is my tweaked solution. var test = async () => { await setTimeout(()=>{console.log('first')}, 1000); console.log ('second') } I've extended the timeout to show it's usefulness. Commented Nov 9, 2019 at 5:57