Last active
May 15, 2017 12:00
-
-
Save valotas/f785f29dff1502366554901ace772716 to your computer and use it in GitHub Desktop.
Promise.inSequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.min.css"> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.min.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine-html.min.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/boot.min.js"></script> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Promise.inSequence</title> | |
</head> | |
<body></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function forEachinSequence(actions) { | |
var last = Promise.resolve(); | |
actions.forEach(function (next) { | |
last = last.then(function () { | |
return next(); | |
}); | |
}); | |
return last; | |
} | |
function inSequence(actions) { | |
var last = Promise.resolve(); | |
var results = []; | |
actions.forEach(function (next) { | |
last = last.then(function () { | |
return next(); | |
}) | |
.then(function (result) { | |
results.push(result); | |
}); | |
}); | |
return last.then(function () { | |
return results; | |
}); | |
} | |
function recursiveInSequence(actions, results) { | |
results = results || []; | |
var action = actions[0]; | |
return action() | |
.then(function (result) { | |
results.push(result); | |
if (actions.length === 1) { | |
return results; | |
} else { | |
return recursiveInSequence(actions.slice(1), results); | |
} | |
}); | |
} | |
function reduceInSequence(actions) { | |
return actions.reduce(function (last, action) { | |
return last.then(function (results) { | |
return action() | |
.then(function (result) { | |
results.push(result); | |
return results; | |
}); | |
}); | |
}, Promise.resolve([])); | |
} | |
describe('Promise.inSequence', function () { | |
function action(delay, seq) { | |
return function () { | |
var result = "delay-" + delay; | |
return new Promise(function (resolve) { | |
if (seq) { | |
seq.push(result); | |
} | |
resolve(result); | |
}, delay); | |
} | |
} | |
function test(inseq, name, withResults) { | |
describe(name, function () { | |
it("should work with an array of 2 actions", function (done) { | |
var seq = []; | |
inseq([action(50, seq), action(10, seq)]) | |
.then(function () { | |
expect(seq).toEqual(["delay-50", "delay-10"]); | |
done(); | |
}); | |
}); | |
it("should work with an array of more than 2 actions", function (done) { | |
var seq = []; | |
inseq([ | |
action(50, seq), | |
action(10, seq), | |
action(11, seq), | |
action(5, seq)]) | |
.then(function () { | |
expect(seq).toEqual([ | |
"delay-50", | |
"delay-10", | |
"delay-11", | |
"delay-5", | |
]); | |
done(); | |
}); | |
}); | |
if (withResults) { | |
it("should resolve to an array with all the results", function (done) { | |
inseq([ | |
action(1), | |
action(2), | |
action(3) | |
]).then(function (results) { | |
expect(results).toEqual([ | |
"delay-1", | |
"delay-2", | |
"delay-3" | |
]); | |
done(); | |
}); | |
}); | |
} | |
}); | |
} | |
test(forEachinSequence, "forEachinSequence (ignoring results)"); | |
test(inSequence, "inSequence", true); | |
test(recursiveInSequence, "recursiveInSequence", true); | |
test(reduceInSequence, "reduceInSequence", true); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment