Code Monkey home page Code Monkey logo

ngpromisedsl's Introduction

Angular Promise DSL

A small library that adds a number of utility methods to Angular's $q promises.

Usage

Simply add ngPromseDsl as a dependency in your module definition

angular.module('myAwesomeApp', ['ngPromiseDsl'])

Methods

thenSet()

Sets the value the promise resolves to onto the given object under the given property name.

promise.thenSet($scope, 'myValue');

Alternatively, sets a constant value when the promise resolves:

promise.thenSet($scope, 'showSpinner', false);

thenSetAll()

Sets all keys from a $q.all() call to the given object under those same keys. Do note that this will wait for all promises to resolve before setting them, which may appear slower than setting the values individually

$q.all([myPromise, myOtherPromise])
  .thenSetAll($scope);

except()

Alias for .catch(), so it can be used with ES 3 (IE 8)

promise.except(function(error) { $log.error(error); });

exceptSet()

Similar to .thenSet(), only for error cases

promise.exceptSet($scope, 'showError', true);

thenTake()

Picks a given property from the resolved value and passes it to the next callback in the promise chain

promise
  .thenTake('subValue')
  .thenSet($scope, 'value');

thenReturn()

Returns a fixed value on success or failure; useful for converting promises that contain business logic (resolves when successful, rejects when failed) to booleans that can be put on the scope

promise
  .thenReturn(true, false)
  .thenSet($scope, 'wasSuccessful')

thenExec()

Calls the given method with the promise value when it resolves, returning the original value. Useful for chaining and not having to declare an explicit return for the callback

promise.thenExec(calculateTotals);

thenApply()

Calls the given method (by name) on the object the promise resolves to. Useful in combination with Restangular

promise.thenApply('get', 'sub-resource');

thenRedirectTo()

Redirects the user to the given path on success using $location, optionally replacing the url.

promise.thenRedirectTo('/success');
promise.thenRedirectTo('/success', true); // calls $location.replace()

TODO

  • Publish on bower / npm repositories
  • Write installation instructions once there
  • thenLog(logLevel) method that calls $log with the promise result or a constant value

Links

Contributors

License

MIT

ngpromisedsl's People

Contributors

fwielstra avatar ghengeveld avatar lammertw avatar

Watchers

Pranav Ainavolu avatar

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.