Javascript Promises?

Posted on
  • Hi All,

    Would it be possible to have JavaScript Promises in the API for async stuff?

    Cheers!

  • Hi Alex,

    Someone else asked this a while back. Basically Promises don't have to be part of the JavaScript interpreter - Promise can just be a normal JS class that you use.

    I haven't tried it, but there's a smallish promises implementation right here that you should be able to use (although it might need some changes for it to be usable with require).

    I'd be happy to include a library if someone contributed one, but I'm not sure it's worth including in the Espruino interpreter given it can just be pulled in if needed.

  • I've adjusted a promise module for Espruino a while ago, you can have look at it here:
    https://github.com/ifavo/espruino-module­s/blob/master/org.favo.promise.js

    This is how it works:
    https://github.com/ifavo/espruino-module­s/blob/master/org.favo.promise-Examples.­js

    It's maybe not be perfectly optimized for Espruino for now, but I'm using it in different JS based projects for a while now and its doing what its meant to do :)

  • @favo thanks! Would you mind if I put that into a module for Espruino? It'd make it easier for @Alex to use (how do you tend to use that module yourself? Using the 'Projects' option, or linking to the URL?).

    Did you base the module on something else, and do you know what the original license was? If it did get included I don't want to violate anyone's copyright.

  • Hi, so far I've been using this with node:
    https://www.npmjs.com/package/promise

    It's MIT licensed and very good!

  • @Gordon, you are welcome to use it.

    It is based on https://github.com/iskugor/js-promise-si­mple/ and was not put under a specific license. There are a lot of adaptions of the simple promises out there.

    I've put my modules in the default modules folder so it behaves like every other of your modules.

  • To get a better understanding, I tried to get promise-simple in jsfiddle.
    My idea was to use something like this.

        console.log(new Date());
        function t(d) {
            var pd = Promise.defer();
            setTimeout(function() {pd.resolve("first" + d);}, d*1000);
            return pd;
        };
        Promise.when(t(1),t(2)).then(function(re­sults) {console.log(new Date(),results);});
    

    In this example function in then is called after having finished t(1) and t(2).
    In log you can see delay of 2 secs for the slower function.
    To get this running, I had to remove .resolve() for isPromise-part in when function. This problem exists in all sources and examples I found in github.
    Therefore I'm unsure whether this is a correct solution. At least it does what I expect to do.

  • For an alternative and dedicated variation, take a look at Tasker - Task synchronizer - kind of a 'purposed' Promise.

    *EDIT* (at the time of version 1v94 (2017-08-29))

    Version 1v86 (2016-07-05) introduced Promise and Promise has matured thru version 1v91 (2017-01-12). Promise makes life of synchronizing/sequencing of asynchronous tasks/callback of callbacks of callbacks of... much much easier... Promise is practically the only way out of callback-hell.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Javascript Promises?

Posted by Avatar for Alex @Alex

Actions