Code Monkey home page Code Monkey logo

Comments (5)

yortus avatar yortus commented on August 17, 2024

Hi @lucaswxp,

Is the getResults() function in your own API? or are you consuming it from some other API? Depending on which case, you can use it with asyncawait as follows:

Creating getResults() to work like a node-style asynchronous function:

// Define the function
var getResults = async.cps (function (param) {
    // use throw and return stmts in here to pass err/result back to the caller's callback
    if (param !== 'foo') throw new Error('param must equal "foo"');
    var result = await (someAsyncComputation(param));
    return result;
});

// Use the function - its just a normal node-style call
getResults('foo', function (err, result) {
    if (err) /* handle error */
    doSomethingWith(result);
});

Consuming a node-style getResults() asynchronous function:

var Promise = require('bluebird');
var getResultsAsync = Promise.promisify(require('the api containing getResults').getResults);

// Use the function
var myFunction = async (function () {
    var result = await (getResultsAsync());
    ...
});

Note that the next release of asyncawait will be able to await node-style functions directly without the need to promisify them first. But for now, that's how to do it. Has this answered your question?

from asyncawait.

lucaswxp avatar lucaswxp commented on August 17, 2024

Thanks @yortus! But would it also work if my callback only takes one parameter? Like function(result) instead of function(err, result)?

Good to know that feature will be coming in next release!

Btw, getResults() is from my own API.

from asyncawait.

yortus avatar yortus commented on August 17, 2024

@lucaswxp async.cps(...) always creates node-style callbacks, so the callback will always be called with the (err, result) convention, even if an error never occurs. If you want your code to be interoperable in node-land, I think it's best to stick to this convention, as almost everything expects this.

from asyncawait.

lucaswxp avatar lucaswxp commented on August 17, 2024

Yes, of course! I was new to node when designed the API, so didn't know the convention at the time. Thanks for the feedback @yortus.

from asyncawait.

Perlence avatar Perlence commented on August 17, 2024

What if callback expects (err, arg1, arg2, ...)? Returing [result1, result2, ...] from async.cps won't do the trick, obviously. There may be some async.cps variant, e.g. async.starCps.

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.