Skip to content

Instantly share code, notes, and snippets.

@wpchen
Created July 24, 2021 23:27
Show Gist options
  • Select an option

  • Save wpchen/71d72f3fd781f43be40865f46c51c2ab to your computer and use it in GitHub Desktop.

Select an option

Save wpchen/71d72f3fd781f43be40865f46c51c2ab to your computer and use it in GitHub Desktop.
// source https://jsbin.com
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function XinY (state, secs) {
return new Promise(function(resolve, reject) {
let ms = secs * 1000;
console.log(`Kicking off ${state} in ${secs}`);
setTimeout(function() {
console.log(`Reached ${state} after ${secs}`);
if (state === 'fail') {
reject('fail');
} else {
resolve(state);
}
}, ms);
});
}
function reflect(p) {
return p.then(
value => ({value: value, status: "fulfilled" }),
error => ({reason: error, status: "rejected" })
);
}
console.log('------ Initializaing -------');
//Promise.all([XinY('a', 1), XinY('fail', 2), XinY('c', 3)].map(reflect))
// .then((results) => { console.log('results'); console.log(results);})
// .catch((errors) => { console.log('errors'); console.log(errors);})
Promise.resolve(4)
.then(
x => { console.log('success: ' + x); return x + 1;},
y => { console.log('failure: ' + y); return -(y + 1);}
)
.then(
x => { console.log('success: ' + x); throw (2*x);},
y => { console.log('failure: ' + y); return -(y + 1);}
)
.catch( function(err) {
console.log('caught error: ' + err);
return 34;
})
.then(
x => { console.log('success: ' + x); return x + 1;},
y => { console.log('failure: ' + y); return -(y + 1);}
)
console.log('------- Waiting...');
</script>
<script id="jsbin-source-javascript" type="text/javascript">
function XinY (state, secs) {
return new Promise(function(resolve, reject) {
let ms = secs * 1000;
console.log(`Kicking off ${state} in ${secs}`);
setTimeout(function() {
console.log(`Reached ${state} after ${secs}`);
if (state === 'fail') {
reject('fail');
} else {
resolve(state);
}
}, ms);
});
}
function reflect(p) {
return p.then(
value => ({value: value, status: "fulfilled" }),
error => ({reason: error, status: "rejected" })
);
}
console.log('------ Initializaing -------');
//Promise.all([XinY('a', 1), XinY('fail', 2), XinY('c', 3)].map(reflect))
// .then((results) => { console.log('results'); console.log(results);})
// .catch((errors) => { console.log('errors'); console.log(errors);})
Promise.resolve(4)
.then(
x => { console.log('success: ' + x); return x + 1;},
y => { console.log('failure: ' + y); return -(y + 1);}
)
.then(
x => { console.log('success: ' + x); throw (2*x);},
y => { console.log('failure: ' + y); return -(y + 1);}
)
.catch( function(err) {
console.log('caught error: ' + err);
return 34;
})
.then(
x => { console.log('success: ' + x); return x + 1;},
y => { console.log('failure: ' + y); return -(y + 1);}
)
console.log('------- Waiting...');</script></body>
</html>
function XinY (state, secs) {
return new Promise(function(resolve, reject) {
let ms = secs * 1000;
console.log(`Kicking off ${state} in ${secs}`);
setTimeout(function() {
console.log(`Reached ${state} after ${secs}`);
if (state === 'fail') {
reject('fail');
} else {
resolve(state);
}
}, ms);
});
}
function reflect(p) {
return p.then(
value => ({value: value, status: "fulfilled" }),
error => ({reason: error, status: "rejected" })
);
}
console.log('------ Initializaing -------');
//Promise.all([XinY('a', 1), XinY('fail', 2), XinY('c', 3)].map(reflect))
// .then((results) => { console.log('results'); console.log(results);})
// .catch((errors) => { console.log('errors'); console.log(errors);})
Promise.resolve(4)
.then(
x => { console.log('success: ' + x); return x + 1;},
y => { console.log('failure: ' + y); return -(y + 1);}
)
.then(
x => { console.log('success: ' + x); throw (2*x);},
y => { console.log('failure: ' + y); return -(y + 1);}
)
.catch( function(err) {
console.log('caught error: ' + err);
return 34;
})
.then(
x => { console.log('success: ' + x); return x + 1;},
y => { console.log('failure: ' + y); return -(y + 1);}
)
console.log('------- Waiting...');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment