All technological notes.
Function Sequence
asynchronous operation:
Asynchronous programming:
asynchronous programming, JavaScript programs can start long-running tasks, and continue running other tasks in paralell.asynchronus programmes are difficult to write and difficult to debug.blocking:
Callback:
Standard contract: error-first, callback-last
(callback) => callback(data)(args, callback) => callback(err, data)Callback functions: functions that are passed as a parameter to another function.
效果: 回调函数将在本函数执行后执行.
list = ["apple", "banana", "cherry"];
// define a asynchronous function
const logEach = (arr, cb) => {
arr.map((row) => {
cb(row);
});
};
// call the asynchronous function
// by defining an anonymous function as callback function
logEach(list, (data) => {
console.log(data);
});
// apple
// banana
// cherry
setTimeout(): 典型回调函数setTimeout():
Syntax:
setTimeout(call_back_function, milliseconds, [param]);
Parameter:
call_back_function:
milliseconds: 1000 = 1 secondparam: a list of parameter to be passed into the call back function
console.log("\n-------- setTimeout --------\n");
// Error: callback function must be funtion(a named function or anonymous function), not codes
// setTimeout(console.log("SetTimeout"), 1000);
const cbFunc = (data) => {
console.log(data);
};
setTimeout(cbFunc, 1000, "SetTimeout"); // SetTimeout;
setTimeout(cbFunc, 1000, "SetTimeout", "Hi"); // SetTimeout;
// "Hi" will not pass because callback function defined with only one parameter
console.log("\n-------- setTimeout --------\n");
// define a function used as callback function latter
const cbFunc = (data) => {
console.log(data);
};
// define an asynchronous function using setTimeout() and callback function
const main = (cb, str) => {
setTimeout(cb, 1000, str);
};
// call asynchronous function
main(cbFunc, "hello");
经典题型
// Q: 执行顺序
console.log("1");
setTimeout(() => console.log("2"), 1000);
console.log("3");
/*
1
3
2
*/
// Q: 执行顺序
console.log("1");
setTimeout(() => console.log("2"), 0);
console.log("3");
// 注意: 即使是0秒,也会延后执行, 因为setTimeout是macroStack, 会take time
/*
1
3
2
*/
Promise:
Promise object state
Pending: The initial state-the operation has not completed yet.Fulfilled/Resolved: The operation has completed successfully, and the promise now has a resolved value.Rejected: The operation has failed, and the promise has a reason for the failure. This reason is usually an Error of some kind.Syntax
// create a promise
let myPromise = new Promise((resolve, reject) => {
// Promise code here
//resolve(data);
//reject(new Error());
});
// call a promise
myPromise.then(() => {}).catch(() => {});
console.log("\n-------- Promise --------\n");
const waitSecond = (ms) =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});
console.log("1");
waitSecond(100)
.then(() => {
console.log("2");
})
.then(() => {
console.log("3");
});
console.log("4");
// 1
// 4
// 2
// 3
.then(): performs operations on a fulfilled promise object and returns a new value..catch(): handles any anticipated promise rejections and throws an error..finally(): executes without regard to whether the promise was fulfilled or rejected.console.log("\n-------- Promise --------\n");
// Promise
const delaySecond = (sec) =>
new Promise((resolve) => {
console.log("delay second:", sec);
setTimeout(resolve, sec, sec); //the seconde sec passes it to resolve function
});
delaySecond(1000)
.then((data) => delaySecond(data)) //data will be 1000 because setTimeout in delaySecond() pass sec to resolve function
.then((data) => delaySecond(data))
.then((data) => delaySecond(data))
.finally(() => console.log("End"));
// delay second: 1000
// delay second: 1000
// delay second: 1000
// delay second: 1000
// End
all()all(): returns a new Promise that can be accessed as an array of resolved values of fulfulled Promises.
// all()
const xFunc = (x) => x / 100;
const yFunc = (y) => y / 1000;
const sum = (arr) =>
new Promise((resolve) => {
const s = arr.reduce((acc, item) => acc + item, 0);
resolve(s);
});
Promise.all([xFunc(3000), yFunc(3000)])
.then((data) => sum(data))
.then(console.log); //33
console.log("\n-------- Promise: mix --------\n");
const delaySecond = (sec) =>
new Promise((resolve) => {
console.log("delay second:", sec);
setTimeout(resolve, sec, sec);
});
const xFunc = (x) => x / 100;
const yFunc = (y) => y / 1000;
const sum = (arr) =>
new Promise((resolve) => {
console.log(arr); // display result array of Promise.all()
const s = arr.reduce((acc, item) => acc + item, 0);
resolve(s);
});
// delay 2 second, then wait until xFunc and yFunc finish, then sum, then log
delaySecond(2000)
.then((data) => Promise.all([xFunc(data), yFunc(data)]))
.then((data) => sum(data))
.then((data) => console.log(data));
// delay second: 2000
// [ 20, 2 ]
// 22
async/awaita new syntax for using promises
async
await
Problems of callbacks, Promise, async/await:
console.log("\n-------- async/await --------\n");
// define a promise
const delaySecond = (sec) =>
new Promise((resolve) => {
console.log("delay second:", sec);
setTimeout(resolve, sec, sec); // the second sec passes to resolve function, eventually is the result of the await function
});
// define an async function
const mainChain = async (ms) => {
const ms1 = await delaySecond(ms);
const ms2 = await delaySecond(ms1); //using the result of previous delaySecond
const ms3 = await delaySecond(ms2);
console.log("End");
};
// call the async function
mainChain(2000);
// delay second: 2000
// delay second: 2000
// delay second: 2000
// End
console.log("\n-------- async/await: all() --------\n");
const xFunc = (x) => x / 100;
const yFunc = (y) => y / 1000;
const sum = (arr) =>
new Promise((resolve) => {
const s = arr.reduce((acc, item) => acc + item, 0);
resolve(s);
});
// define an async using await and Promise.all()
const mainAll = async (ms) => {
const arr = await Promise.all([xFunc(ms), yFunc(ms)]);
console.log(arr);
const sumArr = await sum(arr);
console.log(sumArr);
};
// call async function
mainAll(3000);
// [ 30, 3 ]
// 33
console.log("\n-------- async/await: mix --------\n");
const xFunc = (x) => x / 100;
const yFunc = (y) => y / 1000;
// define a promise function
const delaySecond = (sec) =>
new Promise((resolve) => {
console.log("delay second:", sec);
setTimeout(resolve, sec, sec); // the second sec passes to resolve function, eventually is the result of the await function
});
// define a promise function
const sum = (arr) =>
new Promise((resolve) => {
console.log(arr);
const s = arr.reduce((acc, item) => acc + item, 0);
resolve(s);
});
// define an async function
const mainMix = async (ms) => {
const sec = await delaySecond(ms);
const arr = await Promise.all([xFunc(sec), yFunc(sec)]);
const sumArr = await sum(arr);
console.log(sumArr);
};
// call async function
mainMix(2000);
// delay second: 2000
// [ 20, 2 ]
// 22