Skip to content

Instantly share code, notes, and snippets.

@tieorange
Created December 18, 2018 09:57
Show Gist options
  • Save tieorange/ec2765a164685b39f174f93766bfcc5a to your computer and use it in GitHub Desktop.
Save tieorange/ec2765a164685b39f174f93766bfcc5a to your computer and use it in GitHub Desktop.
Last login: Sat Dec 15 20:13:38 on ttys000
~
➜ node
> function* gf() {
... let a = yield 1;
... console.log('a:', a);
... let b = yield 2;
... console.log('b:', b);
... return 3;
... }
undefined
> g = gf();
Object [Generator] {}
> g
Object [Generator] {}
> g.next()
{ value: 1, done: false }
> g.next()
a: undefined
{ value: 2, done: false }
> g.next()
b: undefined
{ value: 3, done: true }
> g = gf();
Object [Generator] {}
> g.next(100)
{ value: 1, done: false }
> g.next(200)
a: 200
{ value: 2, done: false }
> g.next(300)
b: 300
{ value: 3, done: true }
> g = gf();
Object [Generator] {}
> g.
g.__defineGetter__ g.__defineSetter__ g.__lookupGetter__
g.__lookupSetter__ g.__proto__ g.hasOwnProperty
g.isPrototypeOf g.propertyIsEnumerable g.toLocaleString
g.toString g.valueOf
g.constructor g.next g.return
g.throw
> g.throw(10)
Thrown: 10
> g.next(300)
{ value: undefined, done: true }
> g.next(300)
{ value: undefined, done: true }
> g.next(300)
{ value: undefined, done: true }
> function* f() {
...
> let t = n => new Promise(res => setTimeout(() => res(100), n);
let t = n => new Promise(res => setTimeout(() => res(100), n);
^
SyntaxError: missing ) after argument list
> let t = n => new Promise(res => setTimeout(() => res(100), n));
undefined
> p=t(2000)
Promise {
<pending>,
domain:
Domain {
domain: null,
_events:
{ removeListener: [Function: updateExceptionCapture],
newListener: [Function: updateExceptionCapture],
error: [Function: debugDomainError] },
_eventsCount: 3,
_maxListeners: undefined,
members: [] } }
> t(2000).then(x=>console.log(x));
Promise {
<pending>,
domain:
Domain {
domain: null,
_events:
{ removeListener: [Function: updateExceptionCapture],
newListener: [Function: updateExceptionCapture],
error: [Function: debugDomainError] },
_eventsCount: 3,
_maxListeners: undefined,
members: [] } }
> 100
> gg = function* () {
... let a = yield t(2000);
... console.log('a', a);
... }
[GeneratorFunction: gg]
> g=gg();
Object [Generator] {}
> z=g.next()
{ value:
Promise {
<pending>,
domain:
Domain {
domain: null,
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
members: [] } },
done: false }
> z.then(v => g.next(v));
TypeError: z.then is not a function
> z
{ value:
Promise {
100,
domain:
Domain {
domain: null,
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
members: [] } },
done: false }
> z.value.then(v => g.next(v));
Promise {
<pending>,
domain:
Domain {
domain: null,
_events:
{ removeListener: [Function: updateExceptionCapture],
newListener: [Function: updateExceptionCapture],
error: [Function: debugDomainError] },
_eventsCount: 3,
_maxListeners: undefined,
members: [] } }
> a 100
> mkdir gen
mkdir gen
^^^
SyntaxError: Unexpected identifier
> %
~ took 11m 56s
➜ mkdir gen
~
➜ xs gen
zsh: command not found: xs
~
➜ cd gen
~/gen
➜ npm init -y
Wrote to /Users/tieorange/gen/package.json:
{
"name": "gen",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
~/gen is 📦 v1.0.0 via ⬢ v10.10.0
➜ npm i bluebird
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] No description
npm WARN [email protected] No repository field.
+ [email protected]
added 1 package from 1 contributor and audited 1 package in 1.784s
found 0 vulnerabilities
~/gen is 📦 v1.0.0 via ⬢ v10.10.0 took 2s
➜ node
> const { coroutine } = require('bluebird');
undefined
> f = coroutine(function* () {
... console.log('a'); let a = yield t(3000);
... console.log('b', a);
... }
... );
[Function]
> f();
a
Promise {
_bitField: 17825792,
_fulfillmentHandler0:
ReferenceError: t is not defined
at repl:2:27
at Generator.next (<anonymous>)
at Generator.tryCatcher (/Users/tieorange/gen/node_modules/bluebird/js/release/util.js:16:23)
at PromiseSpawn._promiseFulfilled (/Users/tieorange/gen/node_modules/bluebird/js/release/generators.js:97:49)
at /Users/tieorange/gen/node_modules/bluebird/js/release/generators.js:201:15
at repl:1:1
at Script.runInThisContext (vm.js:96:20)
at REPLServer.defaultEval (repl.js:329:29)
at bound (domain.js:396:14)
at REPLServer.runBound [as eval] (domain.js:409:12)
at REPLServer.onLine (repl.js:627:10)
at REPLServer.emit (events.js:187:15)
at REPLServer.EventEmitter.emit (domain.js:442:20)
at REPLServer.Interface._onLine (readline.js:290:10)
at REPLServer.Interface._line (readline.js:638:8)
at REPLServer.Interface._ttyWrite (readline.js:919:14),
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
> Unhandled rejection ReferenceError: t is not defined
at repl:2:27
at Generator.next (<anonymous>)
at Generator.tryCatcher (/Users/tieorange/gen/node_modules/bluebird/js/release/util.js:16:23)
at PromiseSpawn._promiseFulfilled (/Users/tieorange/gen/node_modules/bluebird/js/release/generators.js:97:49)
at /Users/tieorange/gen/node_modules/bluebird/js/release/generators.js:201:15
at repl:1:1
at Script.runInThisContext (vm.js:96:20)
at REPLServer.defaultEval (repl.js:329:29)
at bound (domain.js:396:14)
at REPLServer.runBound [as eval] (domain.js:409:12)
at REPLServer.onLine (repl.js:627:10)
at REPLServer.emit (events.js:187:15)
at REPLServer.EventEmitter.emit (domain.js:442:20)
at REPLServer.Interface._onLine (readline.js:290:10)
at REPLServer.Interface._line (readline.js:638:8)
at REPLServer.Interface._ttyWrite (readline.js:919:14)
(To exit, press ^C again or type .exit)
> let t = n => new Promise(res => setTimeout(() => res(100), n));
undefined
> f();
a
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
> b 100
>
(To exit, press ^C again or type .exit)
> async function af() {
... console.log('a');
... let a = await t();
...
...
> async function af() {
... console.log('a');
... let a = await t(3000);
... console.log('b', a);
... }
undefined
> af();
a
Promise {
<pending>,
domain:
Domain {
domain: null,
_events:
{ removeListener: [Function: updateExceptionCapture],
newListener: [Function: updateExceptionCapture],
error: [Function: debugDomainError] },
_eventsCount: 3,
_maxListeners: undefined,
members: [] } }
> b 100
> (async () => { console.log('x'); await af(); console.log('y'); })();
x
a
Promise {
<pending>,
domain:
Domain {
domain: null,
_events:
{ removeListener: [Function: updateExceptionCapture],
newListener: [Function: updateExceptionCapture],
error: [Function: debugDomainError] },
_eventsCount: 3,
_maxListeners: undefined,
members: [] } }
> b 100
y
> f = coroutine(function* () {
... ... console.log('a'); let a = yield t(3000);
... console.log('a'); let a = yield t(3000);
^^^
SyntaxError: Unexpected token ...
> ... console.log('b', a);
... console.log('b', a);
^^^
SyntaxError: Unexpected token ...
> ... }
... }
^
SyntaxError: Unexpected token }
> (async () => { console.log('x'); await f(); console.log('y'); })();
x
a
Promise {
<pending>,
domain:
Domain {
domain: null,
_events:
{ removeListener: [Function: updateExceptionCapture],
newListener: [Function: updateExceptionCapture],
error: [Function: debugDomainError] },
_eventsCount: 3,
_maxListeners: undefined,
members: [] } }
> b 100
y
> (async () => { for (let i = 0; i < 10; i++) { console.log('x'); await af(); } })();
x
a
Promise {
<pending>,
domain:
Domain {
domain: null,
_events:
{ removeListener: [Function: updateExceptionCapture],
newListener: [Function: updateExceptionCapture],
error: [Function: debugDomainError] },
_eventsCount: 3,
_maxListeners: undefined,
members: [] } }
> b 100
x
a
b 100
x
a
b 100
x
a
> ddddb 100
x
a
> b 100
x
a
b 100
x
a
dddb 100
x
a
> b 100
x
a
b 100
x
a
>
> b 100
>
> x1 = (async () => { for (let i = 0; i < 10; i++) { console.log('x'); await af(); } });
[AsyncFunction: x1]
> x2 = (async () => { for (let i = 0; i < 10; i++) { console.log('y'); await f(); } });
[AsyncFunction: x2]
>
>
> x1(); x2();
x
a
y
a
Promise {
<pending>,
domain:
Domain {
domain: null,
_events:
{ removeListener: [Function: updateExceptionCapture],
newListener: [Function: updateExceptionCapture],
error: [Function: debugDomainError] },
_eventsCount: 3,
_maxListeners: undefined,
members: [] } }
>
>
>
> b 100
x
a
b 100
y
a
b 100
x
a
b 100
y
a
b 100
x
a
b 100
y
a
b 100
x
a
b 100
y
a
b 100
x
a
b 100
y
a
b 100
x
a
b 100
y
a
b 100
x
a
b 100
y
a
b 100
x
a
b 100
y
a
b 100
x
a
b 100
y
a
b 100
b 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment