-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* github license is asl, so make match * new license * try
- Loading branch information
Showing
3 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
// basic promise | ||
// This file used to be shipped as part of splainer-search, along with bind.js | ||
// It is unclear if we need it... Appears it is required by splainer. But adding | ||
// to splainer didn't work. | ||
(function() { | ||
var Promise = function(taskFn, taskThis, taskArgs) { | ||
this.completed = false; | ||
// when taskFn signals done, do this | ||
this.$$myFn = taskFn; | ||
this.then = function(nextTaskFn, nextTaskThisOrArgs, nextTaskArgs) { | ||
if (nextTaskThisOrArgs instanceof Array) { | ||
nextTaskArgs = nextTaskThisOrArgs; | ||
nextTaskThisOrArgs = undefined; | ||
} | ||
this.next = new Promise(nextTaskFn, nextTaskThisOrArgs, nextTaskArgs); | ||
if (this.completed) { | ||
this.completer(); | ||
} | ||
return this.next; | ||
}; | ||
|
||
// Run the underlying task | ||
this.apply = function() { | ||
taskFn.promise = this; // somebody then(...) me! | ||
taskFn.apply(taskThis, taskArgs); | ||
}; | ||
|
||
// We're done, the next thing can run | ||
this.completer = function() { | ||
this.completed = true; | ||
if (this.next) { | ||
this.next.apply(); | ||
this.completed = false; | ||
} | ||
}; | ||
this.complete = this.completer.bind(this); | ||
}; | ||
|
||
Promise.create = function(func) { | ||
if (func.hasOwnProperty('promise')) { | ||
// I already have a stub promise waiting for | ||
// somebody to call then on | ||
return func.promise; | ||
} else { | ||
var firstPromise = new Promise(); | ||
return firstPromise; | ||
} | ||
}; | ||
window.Promise = Promise; | ||
}()); | ||
|
||
// I have an easier time thinking as an implementor | ||
// in terms of a sequence of asynchronous tasks to be | ||
// chained |