Coder Social home page Coder Social logo

taikuukaits / q-with Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kriskowal/q

0.0 0.0 0.0 1.34 MB

A promise library for JavaScript with thenWith support.

Home Page: http://documentup.com/kriskowal/q/

License: MIT License

JavaScript 97.16% CSS 2.35% HTML 0.49%

q-with's Introduction

This is two small changes to Q that allow for nicer syntax when executing sequential promises with additional dependencies.

Personally not many of my functions take 1 parameter. I wish my code was pure enough for that but it isn't.

What I want to write are promises which take multiple arguments and evaluate in series.

For example, let's say I have a file system and for each operation I need to pass the user making the request (to filter by what they are authorized to see). To init my file browser I want to get a users home folder then get a that folders list of files.

This is what I want:

homeFolderForUser(user).then(function (folder) {
     return filesInFolder(user, folder);
}).then(function (wantFiles, wantInFolder)){
     //...
})

Currently the way to get it is to do this:

homeFolderForUser(user).then(function (folder) {
     return [folder, filesInFolder(user, folder)];
}).spread(function (gotFolder, gotFiles)){
     //...
})

But I think this is ugly and want a prettier way. Introducing .with which creates an array from whatever you resolve with and appends all values you include with. And the sleeker looking thenWith which is really just a wrapper for spread.

homeFolderForUser(user).then(function (folder) {
     return filesInFolder(user, folder).with(folder);
}).thenWith(function (files, folder)){
     //...
})

I chose 'with' because it reads cleanly as 'return promise with value' and I used thenWith to distinguish it from a regular with.

This is a hack on Q and this is how it's implemented:

    //as a new method on promise
    Promise.prototype.with = function (value) {
        if (this.withs) {
            this.withs.push(value);
        } else {
            this.withs = [value];
        }
        return this;
    };

    Promise.prototype.thenWith = Promise.prototype.spread;
//and inside of deferred.resolve()
   if (deferred.promise.withs) {
        become(Q([value].concat(deferred.promise.withs)));
   } else {
        become(Q(value));
   }

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.