Code Monkey home page Code Monkey logo

Comments (4)

yortus avatar yortus commented on July 18, 2024

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.

jmls avatar jmls commented on July 18, 2024

@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.

yortus avatar yortus commented on July 18, 2024

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.

yortus avatar yortus commented on July 18, 2024

Closing as no further action is required

from asyncawait.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.