JavaScript Implementing promise class in javascript

I'm trying to implement a simple promise class with chainable .then() functionality in javascript. Here's what I've done till now -

class APromise {
    constructor(Fn) {
        this.value = null;
        Fn(resolved => { this.value = resolved; });
    }
    then(fn) {
        fn(this.value);
        return this;
    }
}

function myFun() {
    return new APromise(resolve => {
        // just resolve('Hello'); works but this doesn't
        setTimeout(() => { resolve('Hello'); }, 2000);
    });
}

const log = v => { console.log(v); };

myFun().then(log).then(log);

This outputs -

null
null

Instead of 'Hello' 2 times. I think it is currently ignoring setTimeout() call, how should I make this work?

Answer:1

Implementing for old zombie browsers (ES3 to above)

I was using Promise in client js that figured out in old browsers this class doesn't exist. So that I implemented one for them that have resolve and reject methods.

function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function"); 
    }
}
var Promise = function () {
    function Promise(main) {
        var _this = this;

        _classCallCheck(this, Promise);

        this.value = undefined;
        this.callbacks = [];

        var resolve = function resolve(resolveValue) {
            _this.value = resolveValue;

            _this.triggerCallbacks();
        };
        var reject = function reject(rejectValue) {
            _this.value = rejectValue;

            _this.triggerCallbacks();
        };
        main(resolve, reject);
    }

    Promise.prototype.then = function then(cb) {
        var _this2 = this;

        var next = new Promise(function (resolve) {
            _this2.callbacks.push(function (x) {
                return resolve(cb(x));
            });
        });

        return next;
    };

    Promise.prototype.catch = function catch_(cb) {
        var _this2 = this;

        var next = new Promise(function (reject) {
            _this2.callbacks.push(function (x) {
                return reject(cb(x));
            });
        });

        return next;
    };

    Promise.prototype.triggerCallbacks = function triggerCallbacks() {
        var _this3 = this;

        this.callbacks.forEach(function (cb) {
            cb(_this3.value);
        });
    };

    return Promise;
}();
Answer:2

I regularly visit a website with a lot of links of the following format: <a href="SomeURL" title="SomeTitle" id="SomeID">SomeText</a> Sadly the part SomeText is useless in most cases and ...

I regularly visit a website with a lot of links of the following format: <a href="SomeURL" title="SomeTitle" id="SomeID">SomeText</a> Sadly the part SomeText is useless in most cases and ...

  1. javascript replace link text
  2. javascript change link text

Currently in Ramda if I want to deep merge (right) multiple objects I.... var a = _.mergeDeepRight( { one: 1 }, { two: { three: 3 } } ) var b = _.mergeDeepRight( a, { three: { four: 4 } } ) var c = _....

Currently in Ramda if I want to deep merge (right) multiple objects I.... var a = _.mergeDeepRight( { one: 1 }, { two: { three: 3 } } ) var b = _.mergeDeepRight( a, { three: { four: 4 } } ) var c = _....

Example given in JavaScript: Suppose we have two arrays [0,0,0] and [1,1,1]. What's the algorithm to produce all possible ways these two arrays can be combine. Example: mergeEveryWayPossible([0,0,0],...

Example given in JavaScript: Suppose we have two arrays [0,0,0] and [1,1,1]. What's the algorithm to produce all possible ways these two arrays can be combine. Example: mergeEveryWayPossible([0,0,0],...

  1. algorithm combine arrays
  2. merge algorithm array
  3. algorithm merge sorted arrays
  4. algorithm merge two arrays
  5. algorithm merge two sorted arrays
  6. algorithm to merge two arrays in c

Is this recursion coded wrong or is it just that console.log() is not always executed even if the recursion is executed?   function testrecur(s) { console.log("begin testrecur=" + s); s++...

Is this recursion coded wrong or is it just that console.log() is not always executed even if the recursion is executed?   function testrecur(s) { console.log("begin testrecur=" + s); s++...

  1. trigger dataflow from cloud function
  2. send email from cloud function
  3. query firestore from cloud function
  4. trigger dataflow from cloud function python
  5. run dataflow from cloud function
  6. get response from cloud function
  7. write to firestore from cloud function
  8. firebase send notification from cloud function
  9. call cloud function from cloud function