Comments (4)
Hi @jmls,
If I understand correctly, you want to switch some lib functions from being synchronous to being async, without the caller seeing anything different. So, the caller still treats lib.myFoo()
as if it was a synchronous function.
I doubt you will get that to work, either with asyncawait
or generators or any other approach. If lib.myFoo()
becomes asynchronous, it can only return a Promise of a result, not the result itself. The caller will have to deal with that Promise instead of getting the result directly.
There is still a chance of getting it to work with asyncawait
if you control the top-level. i.e if your code calls the third-party lib which then calls lib.myFoo
. If that's the case I can give more info on making it work.
from asyncawait.
@yortus , I think that I can control the top-level - I require the third-party code, is that good enough ? Thanks for the quick response
from asyncawait.
OK, then you may be able to get it working with asyncawait
, because it supports deep coroutines. Generators and ES7 async/await are based on shallow coroutines so those approaches will not work for your situation. I'll try to explain below.
Method
(1) Write myFoo
using asyncawait
. ie:
var async = require('asyncawait/async');
var await = require('asyncawait/await');
var myFoo = async(function (id) {
// DB calls in here using await(...)
});
(2) At your top-level, ensure all calls to the third party library occur inside one (or more) async(...)
functions. If you give more info about the third party library I can give an example of what I mean here.
(3) That's it. See how it works.
Explanation
With ES7 async/await (and similarly for ES6 generators), you can only use await
directly inside the function that is declared async
. If your async foo () {...}
function calls bar()
, bar
cannot use await
in it's body (unless it is also directly declared as async
). That's what is meant by shallow coroutines - they only work at the current stack depth and no deeper.
A deep coroutine on the other hand can await
at any stack depth. So if async foo () {...}
function calls bar()
and bar
calls baz()
, then the bar
and baz
functions can both contain await(...)
expressions, even if they are not declared with async(...)
, because they are running inside the deep coroutine created by the foo
call. Does that make sense?
This provides a potential solution to your problem. foo
is a function at your top-level, bar
is the third party call, and baz
is your lib.myFoo
. The top-level can be declared with async
, the bottom level can use await
, and the middle level (the third party code) can run in between as if everything is synchronous.
from asyncawait.
Closing as no further action is required
from asyncawait.
Related Issues (20)
- Fibers are more flexible(?) HOT 18
- Use with ES6 classes #2 HOT 1
- Not working on nw.js HOT 1
- Error: await functions, yield functions, and value-returning suspendable functions. HOT 1
- setTimout example? HOT 2
- Unexpected token function HOT 6
- Fiber not running HOT 1
- why so long time require this two packages? HOT 1
- Extra parentheses? HOT 2
- fibers v1.0.16 dependency breaks node 4.x HOT 1
- Using a for loop HOT 2
- await inside an awaited function
- Stuck within await()... HOT 2
- Installation issue on node version 9.0.0 HOT 1
- Using forEach with Callbacks function
- Not an issue, just a thank you. HOT 1
- asyncawait fails to build on Ubuntu 16.04.4 LTS HOT 3
- Examples in typescript HOT 1
- Upgrade fibers package for nodejs 10.x or 12.x HOT 6
- Await behavior in non Async function or normal function HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asyncawait.