or ... you could bypass these low-level promise-abstractions altogether, and 
elegantly solve the actual integration-level task asked by your pm - with a 
single, easy-to-debug, throwaway recursive-callback (which will likely work 
exactly the same 10 years from now, while god-knows how the magic will evolve 
with promises in that time).

```javascript
/*
 * example.js
 *
 * example usage:
 * $ FOR_HOW_LONG=1000 node example.js
 *
 * example output:
 * 0 ms - start recusive-callback
 * 1005 ms - waited for server and db to initialize
 * 1007 ms - read file example.js which had 2519 characters
 * 1844 ms - fetched 1270 bytes of data from https://www.example.com
 * 1844 ms - finished recursive-callback
 */

/*jslint
    bitwise: true,
    browser: true,
    maxerr: 4,
    maxlen: 100,
    node: true,
    nomen: true,
    regexp: true,
    stupid: true
*/
'use strict';
var modeNext, onNext, timeStart, tmp;

onNext = function (error, data) {
    // For reject:
    if (error) {
        console.error(error);
        return;
    }
    modeNext += 1;
    switch (modeNext) {
    case 1:
        timeStart = Date.now();
        console.log((Date.now() - timeStart) + ' ms - ' +
            'start recusive-callback');
        // sleep
        setTimeout(onNext, process.env.FOR_HOW_LONG);
        break;
    case 2:
        console.log((Date.now() - timeStart) + ' ms - ' +
            'waited for server and db to initialize');
        onNext();
        break;
    case 3:
        // read this file
        require('fs').readFile(__filename, 'utf8', onNext);
        break;
    case 4:
        console.log((Date.now() - timeStart) + ' ms - ' +
            'read file ' + __filename + ' which had ' + data.length + ' 
characters');
        onNext();
        break;
    case 5:
        // fetch data from https://www.example.js
        require('https').request(
            require('url').parse('https://www.example.com'),
            function (response) {
                onNext(null, response);
            }
        )
            // handle http-request error
            .on('error', onNext)
            .end();
        break;
    case 6:
        // handle http-response error
        data.on('error', onNext);
        // collect http-response chunks
        tmp = [];
        data.on('data', function (chunk) {
            tmp.push(chunk);
        });
        // concatenate http-response chunks
        data.on('end', function () {
            onNext(null, Buffer.concat(tmp));
        });
        break;
    case 7:
        console.log((Date.now() - timeStart) + ' ms - ' +
            'fetched ' + data.length + ' bytes of data from 
https://www.example.com');
        onNext();
        break;
    case 8:
        console.log((Date.now() - timeStart) + ' ms - ' +
            'finished recursive-callback');
        break;
    }
};

modeNext = 0;
onNext();
```

> On 13 Apr 2018, at 12:07 AM, Luiz Felipe Frazão Gonçalves 
> <luizfelipefrazaogoncal...@gmail.com> wrote:
> 
> One new proposal for EcmaScript.
> 
> Promised Functions
> 
> Like async/await, the promised functions would be preceded by a keyword. In 
> the case, promised, it would change the default behavior of the function, 
> making it behave as a promise.
> 
> I will use as an example a classic sleep function:
> 
> function sleep(forHowLong) {
>   return new Promise((resolve, reject) => {
>     setTimeout(function() {
>       resolve();
> 
>       /**
>        * For reject:
>        * 
>        * reject(Error('Some error'));
>        */
>     }, forHowLong);
>   });
> }
> I think to avoid the huge amount of callbacks, there should be a syntax 
> similar to this:
> 
> promised function sleep(forHowLong) {
>   setTimeout(function() {
>     this.resolve(); // could even create a keyword like "resolve"
> 
>     /**
>      * For reject:
>      * 
>      * this.reject(Error('Some error'));
>      */
>   }, forHowLong);
> }
> Note that the hypothetical keyword "promised" before the function statement 
> makes it act as a promise.
> 
> Just a crazy idea I had. :)
> _______________________________________________
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss


> On 13 Apr 2018, at 12:19 AM, Tab Atkins Jr. <jackalm...@gmail.com> wrote:
> 
> On Thu, Apr 12, 2018 at 9:07 AM, Luiz Felipe Frazão Gonçalves
> <luizfelipefrazaogoncal...@gmail.com 
> <mailto:luizfelipefrazaogoncal...@gmail.com>> wrote:
>> One new proposal for EcmaScript.
>> 
>> Promised Functions
>> 
>> Like async/await, the promised functions would be preceded by a keyword. In
>> the case, promised, it would change the default behavior of the function,
>> making it behave as a promise.
>> 
>> I will use as an example a classic sleep function:
>> 
>> function sleep(forHowLong) {
>>  return new Promise((resolve, reject) => {
>>    setTimeout(function() {
>>      resolve();
>> 
>>      /**
>>       * For reject:
>>       *
>>       * reject(Error('Some error'));
>>       */
>>    }, forHowLong);
>>  });
>> }
>> 
>> I think to avoid the huge amount of callbacks, there should be a syntax
>> similar to this:
> 
> Some of these callbacks aren't necessary:
> 
> function sleep(forHowLong) {
>  return new Promise(resolve=>setTimeout(resolve, forHowLong));
> }
> 
> Tho even if you do use all the same callbacks, just formatting it
> properly helps:
> 
> function sleep(forHowLong) {
>  return new Promise(resolve=>setTimeout(_=>resolve(), forHowLong));
> }
> 
> ~TJ
> _______________________________________________
> es-discuss mailing list
> es-discuss@mozilla.org <mailto:es-discuss@mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss 
> <https://mail.mozilla.org/listinfo/es-discuss>
> On 13 Apr 2018, at 1:55 AM, Michał Wadas <michalwa...@gmail.com> wrote:
> 
> Is it codyfing of glorious The Deferred anti-pattern? 
> https://github.com/petkaantonov/bluebird/wiki/Promise-Anti-patterns#the-deferred-anti-pattern
>  
> <https://github.com/petkaantonov/bluebird/wiki/Promise-Anti-patterns#the-deferred-anti-pattern>
> 
> On Thu, Apr 12, 2018 at 6:07 PM, Luiz Felipe Frazão Gonçalves 
> <luizfelipefrazaogoncal...@gmail.com 
> <mailto:luizfelipefrazaogoncal...@gmail.com>> wrote:
> One new proposal for EcmaScript.
> 
> Promised Functions
> 
> Like async/await, the promised functions would be preceded by a keyword. In 
> the case, promised, it would change the default behavior of the function, 
> making it behave as a promise.
> 
> I will use as an example a classic sleep function:
> 
> function sleep(forHowLong) {
>   return new Promise((resolve, reject) => {
>     setTimeout(function() {
>       resolve();
> 
>       /**
>        * For reject:
>        * 
>        * reject(Error('Some error'));
>        */
>     }, forHowLong);
>   });
> }
> I think to avoid the huge amount of callbacks, there should be a syntax 
> similar to this:
> 
> promised function sleep(forHowLong) {
>   setTimeout(function() {
>     this.resolve(); // could even create a keyword like "resolve"
> 
>     /**
>      * For reject:
>      * 
>      * this.reject(Error('Some error'));
>      */
>   }, forHowLong);
> }
> Note that the hypothetical keyword "promised" before the function statement 
> makes it act as a promise.
> 
> Just a crazy idea I had. :)
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss@mozilla.org <mailto:es-discuss@mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss 
> <https://mail.mozilla.org/listinfo/es-discuss>
> 
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to