n-riesco / ijavascript

IJavascript is a javascript kernel for the Jupyter notebook
Other
2.18k stars 187 forks source link

Promises and async/await #73

Closed jmatsushita closed 7 years ago

jmatsushita commented 8 years ago

This might be more for jp-babel but it would be great (and maybe avoid some of the IIFE boilerplate?) to have async/await style asynchronous code. I don't know if it could help avoid having to declare $$.async(); or the $$ scope passing?

Maybe an intermediary step would be to wrap the whole notebook in a Promise and allow cells to contain promises?

n-riesco commented 8 years ago

On 25/07/16 08:22, Jun Matsushita wrote:

Maybe an intermediary step would be to wrap the whole notebook in a Promise and allow cells to contain promises?

Could you give me an example of how this would be used?

jmatsushita commented 8 years ago

Sure thing! Here's one: https://github.com/iilab/notebooks/blob/master/Promises.ipynb

I use IJavascript and node 6.3. Some interesting things happen there though:

I've also added a little example of what the async version would look like.

jmatsushita commented 8 years ago

I could make some async work with a patched jp-babel! https://github.com/iilab/notebooks/blob/master/Promises-Babel-Async.ipynb

n-riesco commented 8 years ago

The first thing I have to say is that I haven't updated jp-babel yet. jp-babel doesn't understand the new $$.

I'll have a look and see if I can put something together for jp-babel (and jp-coffeescript).

The other thing I've noticed is that the asynchronous code those notebooks uses $$ and console without making a copy. This means the results will be send to the last running cell.

n-riesco commented 8 years ago

@jmatsushita Updating jp-babel is not a minor change. It'll have to wait until I refactor the code in IJavascript so that it can be reused by jp-babel and jp-coffeescript.

3 out of the 4 examples in your notebook can be run in IJavascript. Here's how:

Cell 1

// Block-scoped declarations (let, const, function, class)
// not yet supported outside strict mode
'use strict';

var rp = require('request-promise');

{
    let $$ = global.$$;

    $$.async();

    rp('https://github.com/status').then(r => $$.sendResult(r));
}

Cell 2

// Block-scoped declarations (let, const, function, class)
// not yet supported outside strict mode
'use strict';

var rp = require('request-promise');

{
    let console = global.console;
    let $$ = global.$$;

    $$.async();

    // invoke $$.done to tell IJavascript no execution result will be returned
    rp('https://github.com/status').then(r => console.log(r)).then($$.done);
}

Cell 3

// Block-scoped declarations (let, const, function, class)
// not yet supported outside strict mode
'use strict';

var sleep = require('sleep-promise');

{
    let console = global.console;
    let $$ = global.$$;

    $$.async();

    console.log('Hello, World!');
    sleep(1000).then(r => $$.text('Bye!'));
}
pkese commented 7 years ago

Node 6 should be able to support async/await without babel if the kernel would be run using --harmony flag.

Does anyone know how to pass command line argument to kernel?

n-riesco commented 7 years ago

@pkese There is no option to do that. A quick workaround would be to edit the nel package; more specifically, here.

pkese commented 7 years ago

Thanks @n-riesco -- I managed to find that same line of code myself (although it took me somewhat more time than you ;-) ).

You rock!