[go: up one dir, main page]

Sitemap
codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

The ES6 Promises

3 min readNov 28, 2017

--

Press enter or click to view image in full size

So, What exactly is a Promise?

const isSmallThenTen = (num) => {
return new Promise((resolve, reject) => {
if(num < 10) {
resolve(true)
} else {
reject(false)
}
})
}
isSmallThenTen(9)
.then(res => console.log('The number is smaller then 10'))
.catch(err => console.log('The number is not smaller then 10'))
const timeoutIn = (time) => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(time), time)
})
}
timeoutIn(2000)
.then(res => console.log(`Resloved in ${res/1000} seconds`))

Promise.all

const timeoutIn = (time) => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(time), time)
})
}
const timeoutInArr = [
timeoutIn(2000),
timeoutIn(4000),
timeoutIn(8000)
]
Promise.all(timeoutInArr)
.then(values => {
console.log('All the promises are resolved now you can run your code', values
// your code
})

Really loved this article ?

--

--

codeburst
codeburst

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Manoj Singh Negi
Manoj Singh Negi

Written by Manoj Singh Negi

I write about Javascript http://eepurl.com/co0DYj. Solving people problems using code. Javascript developer, Writer.

Responses (2)