functionotherFunc() { console.log('We are in another function') console.log('Do some stuff') }
console.log('Start')
otherFunc()
console.log('End')
> node app.js Start We are in another function Do some stuff End
ASync code example
Sample 1
// ASync code example
console.log('Start')
setTimeout(() => { console.log('We are in the 2 senconds timeout ') }, 2000)
console.log('End')
> node app.js Start End We are in the 2 senconds timeout
Sample 2
// ASync code example
console.log('Start')
functionloginUser(email, password, callback) { setTimeout(() => { console.log('Now we have the data at 2 seconds:') callback({ userEmail: email, userPassword: password }) }, 2000) }
const user = loginUser('sample@gmail.com', 123456, (x) => { console.log(x) })
console.log('End')
Result:
Start End Now we have the data at 2 seconds: { userEmail: "sample@gmail.com" , userPassword: 123456 }
Sample 3
// ASync code example
console.log('Start')
functionloginUser(email, password, callback) { setTimeout(() => { console.log('Now we have the data at 5 seconds:') callback({ userEmail: email, userPassword: password }) }, 5000) }
functiongetUserVideos(email, callback) { setTimeout(() => { console.log('Now we have the data at 2 seconds:') callback(['video1', 'video2', 'video3']) }, 2000) }
Start End Now we have the data at 5 seconds: { userEmail: "sample@gmail.com" , userPassword: 123456 } Now we have the data at 2 seconds: ["video1" , "video2" , "video3"]
Sample 4
// ASync code example
console.log('Start')
functionloginUser(email, password, callback) { setTimeout(() => { console.log('Now we have the data at 5 seconds:') callback({ userEmail: email, userPassword: password }) }, 5000) }
functiongetUserVideos(email, callback) { setTimeout(() => { console.log('Now we have the data at 2 seconds:') callback(['video1', 'video2', 'video3']) }, 2000) }
functionvideoDetails(video, callback) { setTimeout(() => { console.log('Now we have the data at 1 seconds:') callback('title of the video') }, 1000) }
Start End Now we have the data at 5 seconds: { userEmail: "sample@gmail.com" , userPassword: 123456 } Now we have the data at 2 seconds: [ "video1" , "video2" , "video3" ] Now we have the data at 1 seconds: title of the video Now we have the data at 1 seconds: title of the video Now we have the data at 1 seconds: title of the video
Promise
Sample 1
const promise = newPromise((resolve, reject) => { setTimeout(() => { console.log('got the user at 2 seconds:') resolve({ user: 'ed' }) }, 2000) })
functionloginUser(email, password) { returnnewPromise((resolve, reject) => { setTimeout(() => { console.log('Now we have the data at 5 seconds: ') resolve({ userEmail: email, userPassword: password }) }, 5000) }) }
functiongetUserVideos(email) { returnnewPromise((resolve, reject) => { setTimeout(() => { console.log('Now we have the data at 2 seconds:') resolve(['video1', 'video2', 'video3']) }, 2000) }) }
functionvideoDetails(video) { returnnewPromise((resolve, reject) => { setTimeout(() => { console.log('Now we have the data at 1 seconds:') resolve('title of the video') }, 1000) }) }