Synchronous API: API currently only after the execution is complete, before proceeding to the next line API. From top to bottom, line by line execution.
console.log("one")
console.log("two")
Asynchronous API: The API currently does not block the subsequent execution of the code.
console.log("one") setTimeout ( () => console.log("two"), 3000) console.log("three")
Synchronous asynchronous API API difference (the return value acquisition)
Synchronization API API can get the execution result from the return value, but not the asynchronous API.
// 同步 function sum (a, b) { return a+ b }
// 异步 function getMsg () { setTimeout( function () { console.log('hello node.js') }) }
Asynchronous API to get data (callback)
1 function getMsg (fn) { 2 setTimeout(function () { 3 fn({ 4 msg: 'hello' 5 }) 6 }, 3000) 7 } 8 9 getMsg(function (data) { 10 console.log(data) 11 })