Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async / Await #17

Closed
jmatsushita opened this issue Jun 5, 2017 · 2 comments
Closed

Async / Await #17

jmatsushita opened this issue Jun 5, 2017 · 2 comments

Comments

@jmatsushita
Copy link

jmatsushita commented Jun 5, 2017

Hi there,

It would be nice to be able to use the new async/await syntax. Could it be as simple as wrapping the REPL as mentioned here? tc39/proposal-async-await#9 (comment)

Cheers,

Jun

@jmatsushita
Copy link
Author

This idea is that would allow doing something like this:

let fetch = require('isomorphic-fetch');
var $$html$$ = await fetch('http://google.com');

@n-riesco
Copy link
Owner

n-riesco commented Jun 8, 2017

Since this feature has to be implemented in the NEL package, I've opened an issue there. See n-riesco/nel#6 .


In the meantime, as a workaround, you could define a helper function like this (untested):

function awaitFor(promise, send, $_) {
    send = send || $$.sendResult;
    $_ = $_ || $$;

    $_.async();

    promise.then(function(result) {
        send.call($_, result);
    }).catch(function(error) {
        $_.sendError(error);
    });
}

so that your example would be re-written as:

const fetch = require('isomorphic-fetch');
awaitFor(fetch('http://google.com'), $$.html);

And another example:

const fs = require('fs');
const util = require('util');
const readdir = util.promisify(fs.readdir);

awaitFor(readdir('.'));

@n-riesco n-riesco closed this as completed Jun 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants