Giter Club home page Giter Club logo

promisejs.org's Introduction

promisejs.org

A website dedicated to promises in JavaScript

Build Status Dependency Status

License

MIT

promisejs.org's People

Contributors

anantja-in avatar andreypopp avatar anielsen avatar astorije avatar awareness481 avatar bradjlarson avatar daniel15 avatar davidblurton avatar dongliu avatar edef1c avatar fay-jai avatar forbeslindesay avatar gabrielpconceicao avatar greggman avatar hyungtae3713 avatar i avatar josephfinlayson avatar jsor avatar lacivert avatar mathbruyen avatar mwilliamson avatar mythnc avatar smholloway avatar the-alchemist avatar turquoisemelon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

promisejs.org's Issues

local path in polyfill

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.

The implementation does not pass promise/A+ test suit

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);
             }

Mention jQuery 3's A+ compliance?

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?

How can the "Fulfilling" section code work?

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?

Making it clearer?

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.

Last updated date of Promises article

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.