Do async in a blocking program language way?

蓝咒 提交于 2019-12-02 07:41:52
rsp

My question is, does it have to be in this way? Is it possible by writing sync code but get a async effect?

Yes. You can use async/await on Node 7+ or generator based coroutines on older versions of Node.

It will look something like:

var x = await f1();
var y = await f2(x);
// ...

or even:

var y = await f2(await f1());
// ...

but it will still be asynchronous and non-blocking. You can use that syntax with any function that returns a promise - like a lot of the database drivers and ORMs/ODMs in Node do.

See those answers for more examples:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!