Javascript is single threaded
Blocked, block UI and lots things else
Event -> listener approach
Terminology
Promise is thenable
Creating a Promise
Chaining a Promise
Testing if something is a Promise:
if (Promise.resolve(obj) == obj)`
Promise working with other language features
(rather new in 2017)
Benefit
Concerns
"Gotya"
Here I want to understand javascript Promise, async function, await. First I define a normal function.
function plus(x) {
return x+1;
}
plus(3);
Now I create an async function do the same thing.
async function aPlus(x) {
return x+1;
}
plus;
aPlus;
So, a function is a Function object while an async function is an AsyncFunction. What's the result from calling an async function?
var aResult = aPlus(5);
aResult;
typeof aResult;
Ok, so aResult is an object. What's it? Bring on my inspector!
const util = require('@bingsjs/cli-utils');
util.check(aResult);
So the result is a promise, resolved to 6.
Examine execution, when exactly is the code in async function executed?
async function app(x) {
console.log("app x="+x);
return x+2;
}
resApp = app(4);
resApp;
const util=require('@bingsjs/cli-utils');
async function app(x) { // asynchronous plus plus
console.log("app x="+x);
return x+2;
}
async function delayedApp(x) {
console.log("delayed app x="+x+", now about to make a Promise");
var p = new Promise((resolve, reject)=>{
console.log("This is code inside the Promise, now set timeout");
setTimeout(()=>{
console.log("Time out, now resolve x+2");
resolve(x+2);
}, 10000);
console.log("This is code inside the Promise, after timeout, return something funny");
return "Something Funny"
});
console.log("Promise instance is made, now check Promise and return");
util.check(p);
return p;
}
var resapp;
function checkapp() {
console.log("before calling async function");
resapp = app(7);
util.check(resapp);
console.log("after calling async function");
console.log("before calling delayed async function");
resapp = delayedApp(9);
util.check(resapp);
console.log("after calling delayed async function");
}
checkapp();
before calling async function
app x=7
============================== Start Reporting obj ==============================
obj: Promise { 9 }
typeof obj: object
obj's own properties: []
obj's prototype's own properties: [ 'constructor', 'then', 'catch' ]
obj's prototype's prototype's own properties: [ 'constructor',
'__defineGetter__',
'__defineSetter__',
'hasOwnProperty',
'__lookupGetter__',
'__lookupSetter__',
'isPrototypeOf',
'propertyIsEnumerable',
'toString',
'valueOf',
'__proto__',
'toLocaleString' ]
=============================== End Reporting obj ===============================
after calling async function
before calling delayed async function
delayed app x=9, now about to make a Promise
This is code inside the Promise, now set timeout
This is code inside the Promise, after timeout, return something funny
Promise instance is made, now check Promise and return
============================== Start Reporting obj ==============================
obj: Promise { <pending> }
typeof obj: object
obj's own properties: []
obj's prototype's own properties: [ 'constructor', 'then', 'catch' ]
obj's prototype's prototype's own properties: [ 'constructor',
'__defineGetter__',
'__defineSetter__',
'hasOwnProperty',
'__lookupGetter__',
'__lookupSetter__',
'isPrototypeOf',
'propertyIsEnumerable',
'toString',
'valueOf',
'__proto__',
'toLocaleString' ]
=============================== End Reporting obj ===============================
============================== Start Reporting obj ==============================
obj: Promise { <pending> }
typeof obj: object
obj's own properties: []
obj's prototype's own properties: [ 'constructor', 'then', 'catch' ]
obj's prototype's prototype's own properties: [ 'constructor',
'__defineGetter__',
'__defineSetter__',
'hasOwnProperty',
'__lookupGetter__',
'__lookupSetter__',
'isPrototypeOf',
'propertyIsEnumerable',
'toString',
'valueOf',
'__proto__',
'toLocaleString' ]
=============================== End Reporting obj ===============================
after calling delayed async function
Time out, now resolve x+2
console.log(resapp);
util.check(resapp);
(1) async function defines an AsyncFunction object (2) if called, async function's body is immediately executed (3) if async function body returns a value (not a Promise), it's wrapped in a resolved Promise (4) if async function body returns a Promise, return a Promise (same? not sure, not important) (5) if a Promise is instantiated (new), the code block in the Promise's parameter function body is executed immediately (6) in the Promise's parameter function body, "resolve(something)" is called to resolve the value (7) what about the parameter functioin return something? I don't know. Seems no one care - I returned something but cannot find it anywhere. So, in the parameter function, resolve is important, return is not.