Skip to content

Latest commit

 

History

History
43 lines (38 loc) · 840 Bytes

13_introduction_to_javascript_promise.md

File metadata and controls

43 lines (38 loc) · 840 Bytes

Eventual Results

let add = (a, b) => {
    let sum = a + b;
    console.log("The calculated sum is ", sum)
    return sum
}

add(2, 3)
setTimeout(() => add(3, 3), 5000)

Create and return a resolved Promise

let p = new Promise((resolve, reject) => {
	// Perform async computation
	resolve("Here is resulting data")
})

Create and return a rejected Promise

let p = new Promise((resolve, reject) => {
	// Perform async computation
	reject('The operation failed due to timeout')
}

Creating promise using Promise.resolve

let p = Promise.resolve('Creates a new Promise with resulting value')
p.then(data => {
	console.log(data)
})

Creating promise using Promise.reject

let p = Promise.reject('Creates new Promise containing error')
p.catch(err => {
	console.log(err)
})