Skip to content

Instantly share code, notes, and snippets.

@rickj33
Created March 14, 2015 20:21
Show Gist options
  • Save rickj33/4e8aa00f9721af529ea6 to your computer and use it in GitHub Desktop.
Save rickj33/4e8aa00f9721af529ea6 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/fumulo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// "steps" is an array of functions... work to be done
function run(steps, data, done){
// Run the async function and return a promise.
// The promise will resolve to a rejected rejected
// state if the async function callback is called
// with an err. It will resolve to a fullfield state
// otherwise.
function runStep(fn) {
return new Promise(function(ok, failed) {
fn(data, function(err) {
if (err) {
failed(err);
} else {
ok();
}
});
});
}
// Simply chain the step promises to run them in serial order.
// If one promise resolved to a rejected state, the chain
// will stop and resolved to a rejected state.
return steps.reduce(
function(result, step) {
return result.then(function(){
return runStep(step);
});
},
// Provide an initial promise to start the chain.
Promise.resolve()
// Done is called when the promise chain is fullfilled.
// If its state is rejected, it will receive the error that
// failed the chain.
).then(done, done);
}
var asyncSteps = [1,2,3].map(function(i){
return function(data, done) {
var delay = Math.floor(Math.random() * 1000);
console.log('step #' + i + ' called with ' + data);
console.log('step #' + i + ' will last for ' + delay + 'ms');
setTimeout(function() {
data.push(i*i);
done();
}, delay);
};
});
var result = [];
run(asyncSteps, result, function() {
console.log('Result: ' + result.join(', '));
});
console.log('Task started...');
</script>
<script id="jsbin-source-javascript" type="text/javascript">// "steps" is an array of functions... work to be done
function run(steps, data, done){
// Run the async function and return a promise.
// The promise will resolve to a rejected rejected
// state if the async function callback is called
// with an err. It will resolve to a fullfield state
// otherwise.
function runStep(fn) {
return new Promise(function(ok, failed) {
fn(data, function(err) {
if (err) {
failed(err);
} else {
ok();
}
});
});
}
// Simply chain the step promises to run them in serial order.
// If one promise resolved to a rejected state, the chain
// will stop and resolved to a rejected state.
return steps.reduce(
function(result, step) {
return result.then(function(){
return runStep(step);
});
},
// Provide an initial promise to start the chain.
Promise.resolve()
// Done is called when the promise chain is fullfilled.
// If its state is rejected, it will receive the error that
// failed the chain.
).then(done, done);
}
var asyncSteps = [1,2,3].map(function(i){
return function(data, done) {
var delay = Math.floor(Math.random() * 1000);
console.log('step #' + i + ' called with ' + data);
console.log('step #' + i + ' will last for ' + delay + 'ms');
setTimeout(function() {
data.push(i*i);
done();
}, delay);
};
});
var result = [];
run(asyncSteps, result, function() {
console.log('Result: ' + result.join(', '));
});
console.log('Task started...');</script></body>
</html>
// "steps" is an array of functions... work to be done
function run(steps, data, done){
// Run the async function and return a promise.
// The promise will resolve to a rejected rejected
// state if the async function callback is called
// with an err. It will resolve to a fullfield state
// otherwise.
function runStep(fn) {
return new Promise(function(ok, failed) {
fn(data, function(err) {
if (err) {
failed(err);
} else {
ok();
}
});
});
}
// Simply chain the step promises to run them in serial order.
// If one promise resolved to a rejected state, the chain
// will stop and resolved to a rejected state.
return steps.reduce(
function(result, step) {
return result.then(function(){
return runStep(step);
});
},
// Provide an initial promise to start the chain.
Promise.resolve()
// Done is called when the promise chain is fullfilled.
// If its state is rejected, it will receive the error that
// failed the chain.
).then(done, done);
}
var asyncSteps = [1,2,3].map(function(i){
return function(data, done) {
var delay = Math.floor(Math.random() * 1000);
console.log('step #' + i + ' called with ' + data);
console.log('step #' + i + ' will last for ' + delay + 'ms');
setTimeout(function() {
data.push(i*i);
done();
}, delay);
};
});
var result = [];
run(asyncSteps, result, function() {
console.log('Result: ' + result.join(', '));
});
console.log('Task started...');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment