node-simple-promise

0.0.1 • Public • Published

Node Simple Promise

We all know that after calling async functions after async functions, one after another, you end up getting deeply nested callbacks, making things really difficult to look at, and not to mention, maintain.

If only there was a way to mitigate the deep nesting issue.

Good news is, there is. Node Simple Promise will do just that.

Just grab a copy of Promise through Node.js's require.

Promise = require 'Promise'

Then create a new instance of Promise.

promise = new Promise

Call it's seq() method, and pass in a callback that calls an asynchronous function.

promise.seq ->
	setTimeout =>
		console.log 'Hello, World!'
	, 0

Of course, it will be nice if we can know if the asynchronous function call has finshed doing its stuff.

You can do just that by calling the done() method that the seq() method injects.

promse.seq ->
	setTimeout =>
		console.log 'Hello, World!'
		@done()
	, 0

And the nice thing about done() is that it allows you to execute more than one async functions using multiple seq() calls.

promise.seq ->
	setTimeout =>
		console.log 'Hello, World!'
		@done()
	, 0

.seq ->
	setTimeout =>
		console.log 'Such a nice day!'
		@done()
	, 0

You can also pass in parameters through the done() method, and then grab them from another seq() call.

promise.seq ->
	setTimeout =>
		@done('Hello, World!')
	, 0

.seq (message) ->
	setTimeout (=>
		console.log message
		@done()
	,

And this is how the above code would've looked like without Promise.coffee

setTimeout =>
	message = 'Hello, World!'
	setTimeout =>
		console.log message
	, 0
, 0

"What about JavaScript!? D:"

Don't worry, this module is perfectly compatible with JavaScript

Just be sure to include the CoffeeScript module wherever you include the Node Simple Promise module.

require('coffee');
var Promise = require('Promise');

var promise = new Promise

promise.seq(function () {
	var _this = this;
	setTimeout(function () {
		_this.done('Hello, World!');
	}, 0)
})

.seq(function (message) {
	var _this = this;
	setTimeout(function () {
		console.log(message)
		_this.done()
	}, 0)
});

TODO's

  • Publish on NPM

License

Copyright (c) 2012 Salehen Shovon Rahman

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme

Keywords

none

Package Sidebar

Install

npm i node-simple-promise

Weekly Downloads

1

Version

0.0.1

License

none

Last publish

Collaborators

  • shovonr