Created
September 21, 2017 19:10
-
-
Save cube-drone/8dd8a931f77f7ff7756aac37f7dd118d to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/safazac
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> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
"use strict"; | |
var seriallyApplyTransformations = function seriallyApplyTransformations(transformations, x) { | |
var listOfTransforms = transformations.slice(); // copy array | |
var processTransforms = function processTransforms(x) { | |
if (listOfTransforms.length == 0) { | |
return Promise.resolve(x); | |
} else { | |
return Promise.resolve(listOfTransforms.shift()(x)).then(processTransforms); | |
} | |
}; | |
return processTransforms(x); | |
}; | |
var transformations = [function (x) { | |
return Promise.resolve(x + 1); | |
}, function (x) { | |
return x * 2; | |
}, function (x) { | |
return x / 2; | |
}, function (x) { | |
return Promise.resolve(x - 1); | |
}]; | |
seriallyApplyTransformations(transformations, 20).then(console.log); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
const seriallyApplyTransformations = (transformations, x) => { | |
let listOfTransforms = transformations.slice(); // copy array | |
const processTransforms = (x) => { | |
if(listOfTransforms.length == 0){ | |
return Promise.resolve(x) | |
} | |
else{ | |
return Promise.resolve(listOfTransforms.shift()(x)).then(processTransforms); | |
} | |
} | |
return processTransforms(x); | |
} | |
let transformations = [x => Promise.resolve(x + 1), x => x * 2, x => x/2, x => Promise.resolve(x - 1)]; | |
seriallyApplyTransformations(transformations, 20).then(console.log); | |
</script></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
"use strict"; | |
var seriallyApplyTransformations = function seriallyApplyTransformations(transformations, x) { | |
var listOfTransforms = transformations.slice(); // copy array | |
var processTransforms = function processTransforms(x) { | |
if (listOfTransforms.length == 0) { | |
return Promise.resolve(x); | |
} else { | |
return Promise.resolve(listOfTransforms.shift()(x)).then(processTransforms); | |
} | |
}; | |
return processTransforms(x); | |
}; | |
var transformations = [function (x) { | |
return Promise.resolve(x + 1); | |
}, function (x) { | |
return x * 2; | |
}, function (x) { | |
return x / 2; | |
}, function (x) { | |
return Promise.resolve(x - 1); | |
}]; | |
seriallyApplyTransformations(transformations, 20).then(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment