Closed jmatsushita closed 7 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?
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:
Kernel starting. Please wait...
. Waiting a bit then works and the new notebooks become responsive irrespective of what happens in the Promises notebook.I've also added a little example of what the async
version would look like.
I could make some async work with a patched jp-babel! https://github.com/iilab/notebooks/blob/master/Promises-Babel-Async.ipynb
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.
@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:
// 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));
}
// 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);
}
// 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!'));
}
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?
@pkese There is no option to do that. A quick workaround would be to edit the nel
package; more specifically, here.
Thanks @n-riesco -- I managed to find that same line of code myself (although it took me somewhat more time than you ;-) ).
You rock!
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?