Blame view

node_modules/when/lib/decorators/iterate.js 2.26 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  /** @license MIT License (c) copyright 2010-2014 original author or authors */
  /** @author Brian Cavalier */
  /** @author John Hann */
  
  (function(define) { 'use strict';
  define(function() {
  
  	return function generate(Promise) {
  
  		var resolve = Promise.resolve;
  
  		Promise.iterate = iterate;
  		Promise.unfold = unfold;
  
  		return Promise;
  
  		/**
  		 * @deprecated Use github.com/cujojs/most streams and most.iterate
  		 * Generate a (potentially infinite) stream of promised values:
  		 * x, f(x), f(f(x)), etc. until condition(x) returns true
  		 * @param {function} f function to generate a new x from the previous x
  		 * @param {function} condition function that, given the current x, returns
  		 *  truthy when the iterate should stop
  		 * @param {function} handler function to handle the value produced by f
  		 * @param {*|Promise} x starting value, may be a promise
  		 * @return {Promise} the result of the last call to f before
  		 *  condition returns true
  		 */
  		function iterate(f, condition, handler, x) {
  			return unfold(function(x) {
  				return [x, f(x)];
  			}, condition, handler, x);
  		}
  
  		/**
  		 * @deprecated Use github.com/cujojs/most streams and most.unfold
  		 * Generate a (potentially infinite) stream of promised values
  		 * by applying handler(generator(seed)) iteratively until
  		 * condition(seed) returns true.
  		 * @param {function} unspool function that generates a [value, newSeed]
  		 *  given a seed.
  		 * @param {function} condition function that, given the current seed, returns
  		 *  truthy when the unfold should stop
  		 * @param {function} handler function to handle the value produced by unspool
  		 * @param x {*|Promise} starting value, may be a promise
  		 * @return {Promise} the result of the last value produced by unspool before
  		 *  condition returns true
  		 */
  		function unfold(unspool, condition, handler, x) {
  			return resolve(x).then(function(seed) {
  				return resolve(condition(seed)).then(function(done) {
  					return done ? seed : resolve(unspool(seed)).spread(next);
  				});
  			});
  
  			function next(item, newSeed) {
  				return resolve(handler(item)).then(function() {
  					return unfold(unspool, condition, handler, newSeed);
  				});
  			}
  		}
  	};
  
  });
  }(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); }));