New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

reflux-promise

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reflux-promise

Extension to reflux-core to use Promises.

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

reflux-promise

Plugin for reflux-core to use Promises.

Here are the API docs for reflux-promise.

Installation

You can currently install the package as an npm package.

NPM

The following command installs reflux-promise as an npm package:

npm install reflux-promise

Usage

To install promise functionality do the following in your application's bootstrapper:

import Reflux from "reflux";
import RefluxPromise from "reflux-promise";

// Uses the user agent's Promise implementation
Reflux.use(RefluxPromise(window.Promise));

// Uses Q
import Q from "q";
Reflux.use(RefluxPromise(Q.Promise));

// Uses bluebird
import bluebird from "bluebird";
Reflux.use(RefluxPromise(bluebird))

Extensions to Asynchronous actions

reflux-promise extends asynchronous actions, i.e.:

// this creates 'load', 'load.completed' and 'load.failed'
var { load } = Reflux.createActions({
    "load": {children: ["completed","failed"]}
});

A couple of helper methods are available to trigger the completed and failed child actions:

  • promise

  • listenAndPromise

The following are all equivalent:

// Using load above with a promise here called "apiPromise"
load.listen( function(arguments) {
    apiPromise(arguments)
        .then(load.completed)
        .catch(load.failed);
});

// Can be shortened down to use `promise` like below
load.listen( function(arguments) {
    load.promise( apiPromise(arguments) );
});

// Furthermore with `listenAndPromise`
load.listenAndPromise( apiPromise );

Asynchronous actions as Promises

PublisherMethods#triggerAsync is modified so that asynchronous actions can be used as promises. The following example is for server-side rendering when you must await the successful (or failed) completion of an action before rendering. Suppose you had an action, makeGetRequest, and a store, RequestStore, to make an API request:

// Create async action with `completed` & `failed` children
var makeGetRequest = Reflux.createAction({ asyncResult: true });

var RequestStore = Reflux.createStore({
    init: function() {
        this.listenTo(makeRequest, 'onMakeRequest');
    },

    onMakeGetRequest: function(url) {
        // Assume `request` is some HTTP library (e.g. superagent)
        request.get(url, function(err, res) {
            if (err) {
                return makeGetRequest.failed(err);
            }
            makeGetRequest.completed(body);
        })
    }
});

You could use promises to make the request and either render or serve an error:

makeGetRequest.triggerAsync('/api/something').then(function(body) {
    // Render the response body
}).catch(function(err) {
    // Handle the API error object
});

Colophon

List of contributors is available on Github.

This project is licensed under BSD 3-Clause License. Copyright (c) 2014, Mikael Brassman.

For more information about the license for this particular project read the LICENSE.md file.

Keywords

FAQs

Package last updated on 20 Jan 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc