1new Promise((resolve, reject) => {
2 console.log('Initial');
3
4 resolve();
5})
6.then(() => {
7 throw new Error('Something failed');
8
9 console.log('Do this');
10})
11.catch(() => {
12 console.error('Do that');
13})
14.then(() => {
15 console.log('Do this, no matter what happened before');
16});
17
1try {
2 const result = syncDoSomething();
3 const newResult = syncDoSomethingElse(result);
4 const finalResult = syncDoThirdThing(newResult);
5 console.log(`Got the final result: ${finalResult}`);
6} catch(error) {
7 failureCallback(error);
8}
9
1doSomething()
2.then(result => doSomethingElse(result))
3.then(newResult => doThirdThing(newResult))
4.then(finalResult => {
5 console.log(`Got the final result: ${finalResult}`);
6})
7.catch(failureCallback);
8
1async function foo() {
2 try {
3 const result = await doSomething();
4 const newResult = await doSomethingElse(result);
5 const finalResult = await doThirdThing(newResult);
6 console.log(`Got the final result: ${finalResult}`);
7 } catch(error) {
8 failureCallback(error);
9 }
10}
11
1doSomething(function(result) {
2 doSomethingElse(result, function(newResult) {
3 doThirdThing(newResult, function(finalResult) {
4 console.log('Got the final result: ' + finalResult);
5 }, failureCallback);
6 }, failureCallback);
7}, failureCallback);
8
1doSomething()
2.then(result => doSomethingElse(result))
3.then(newResult => doThirdThing(newResult))
4.then(finalResult => console.log(`Got the final result: ${finalResult}`))
5.catch(failureCallback);
6
1const promise = doSomething();
2const promise2 = promise.then(successCallback, failureCallback);
3
1doSomething()
2.then(function(result) {
3 return doSomethingElse(result);
4})
5.then(function(newResult) {
6 return doThirdThing(newResult);
7})
8.then(function(finalResult) {
9 console.log('Got the final result: ' + finalResult);
10})
11.catch(failureCallback);
12