A website dedicated to promises in JavaScript
MIT
a promise website to document and promote
Home Page: www.promisejs.org
License: MIT License
please replace the key require as another name when build it
browserify has plugin can do it
thankyou
Hi, I translated the index, how is the better way to PR this? New branch? New file?
index-pt-BR.jade?
Look at the bottom of this: https://www.promisejs.org/polyfills/promise-4.0.0.js
That aside, it's all a little too node flavored anyway. Can you just create a version that was specifically created for the browser. No minification of anything please.
very nice walkthrough of the design principals behind it.
principals should probably be principles
Bluebird is a popular Promise library.
https://github.com/petkaantonov/bluebird
It is also the fastest implementation by quite a margin and the one with the best stack traces. It is very applicable in NodeJS.
Please consider adding it to your list of implementations.
On the Patterns page (patterns.jade), in the Parallel operations example, the called var is never set to true so the callback will be called multiple times (including receiving multiple errors).
Based on the presence of a called var, and a check on it, it seems this probably isn't the intention.
The source of promise-4.0.0.js (in polyfills/output, also linked on the site) has a module called 'C:\Users\forbes.lindesay\Documents\GitHub\promisejs.org\node_modules\browserify\node_modules\insert-module-globals\node_modules\process\browser.js'
This should be removed if only for reducing the file size.
Hi
I was wondering which browsers I can use these polyfills on
Specifically, is ie8 supported?
2.2.2.2: it must not be called before `promise` is fulfilled
1) fulfilled after a delay
2.2.3.2: it must not be called before `promise` is rejected
2) rejected after a delay
2.3.1: If `promise` and `x` refer to the same object, reject `promise` with a `TypeError' as the reason.
3) via return from a fulfilled promise
4) via return from a rejected promise
below is my salution
diff --git a/Promise-origin.js b/Promise.js
index 57c1bb1..0a17c3b 100644
--- a/Promise-origin.js
+++ b/Promise.js
@@ -14,21 +14,25 @@ function Promise(fn) {
var handlers = [];
function fulfill(result) {
- if (state === PENDING) {
- state = FULFILLED;
- value = result;
- handlers.forEach(handle);
- handlers = null;
- }
+ setTimeout(function() {
+ if (state === PENDING) {
+ state = FULFILLED;
+ value = result;
+ handlers.forEach(handle);
+ handlers = null;
+ }
+ });
}
function reject(error) {
- if (state === PENDING) {
- state = REJECTED;
- value = error;
- handlers.forEach(handle);
- handlers = null;
- }
+ setTimeout(function() {
+ if (state === PENDING) {
+ state = REJECTED;
+ value = error;
+ handlers.forEach(handle);
+ handlers = null;
+ }
+ });
}
function resolve(result) {
@@ -73,7 +77,9 @@ function Promise(fn) {
function(result) {
if (typeof onFulfilled === 'function') {
try {
- return resolve(onFulfilled(result));
+ var x = onFulfilled(result);
+ if (x === promise) reject(new TypeError('same object'));
+ return resolve(x);
} catch (ex) {
return reject(ex);
}
@@ -84,7 +90,9 @@ function Promise(fn) {
function(error) {
if (typeof onRejected === 'function') {
try {
- return resolve(onRejected(error));
+ var x = onRejected(error);
+ if (x === promise) reject(new TypeError('same object'));
+ return resolve(x);
} catch (ex) {
return reject(ex);
}
From what I understand, jQuery 3 and beyond ships with an A+ compliant promise implementation. Yet, the doc still states:
This feels like a good time to warn you that what jQuery calls a promise is in fact totally different to what everyone else calls a promise. jQuery's promises have a poorly thought out API that will likely just confuse you.
I know it'd be a bad idea to completely remove that as old versions of jQuery will be around for a very long time, but it might be a good idea to raise awareness for the fact that things actually improved in v3?
function* demo() {
var res = yield 10;
assert(res === 32);
return 42;
}
var d = demo();
var resA = d.next();
// => {value: 10, done: false}
var resB = d.next(32);
// => {value: 42, done: true}
//if we call d.next() again it throws an error
demo takes no arguments, and doesn't assign any values out of arguments
so how could the second call to demo.next() possibly pass the assert(res === 32); line?
In the text,
Promises are currently only supported by a pretty small selection of browsers
Please see https://caniuse.com/#search=promise for updated information.
I immediately stopped reading the page because it is clear the information is badly dated. Then I called Carbon, the company that supports this website with ads because it seems they are supporting an abandoned project.
Not sure when I will get a chance to raise a PR.
Reading through this I couldn't help wonder if it's not that clear. To someone not familiar with promises the sync code is super clear both in how it's implemented and how it's used
impl
function readJSONSync(filename) {
return JSON.parse(fs.readFileSync(filename, 'utf8'));
}
usage
var obj = readJSONSync('somefile.json');
// do something with obj
If you're familiar with callbacks the callback version is also clear
impl
function readJSON(filename, callback){
fs.readFile(filename, 'utf8', function (err, res){
if (err) return callback(err);
callback(null, JSON.parse(res));
});
}
usage
readJSON(filename, function(err, obj) {
if (err) { throw; } // or whatever
// do something with obj
});
But even though it's explained indirectly it seems like it's not clear to a noob how to use the promise version.
impl
function readJSON(filename){
return readFile(filename, 'utf8').then(JSON.parse);
}
usage
???
Would it be good to add a usage example?
readJSON("somefile.json").then(function(obj) {
// do something with obj
});
Or possibly
readJSON("somefile.json").then(function(obj) {
// do something with obj
}).catch(function(err) {
// do something about error
});
One difference from the first promise example (the one without catch
) vs the original sync example, the sync example will throw if reading or parsing fails. The promise example will silently fail.
It would be awesome if there was a last date modified date included in the article - of course when developing with newer technologies reading up to date articles is best.
I may fork this and submit a pull request for this feature. Cheers!
http://www.promisejs.org/implementations/
seems to have some implementation examples mixed up, and don't utilize the new constructor api. Interestingly, the project repo appears to be correct. I suspect the site just needs a re-publish.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.