Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/aja.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,43 @@
if(typeof ajaGo[type] === 'function'){
return ajaGo[type].call(this, url);
}
},

/**
* Triggers the call and returns a Promise (thenable)
* Automatically resolves on success, rejects on error.
* This is the end of the chain.
*
* @example aja()
* .url('data.json')
* .promise();
*
* @returns {Promise}
*/
promise : function(){

var self = this;

if(typeof Promise === "undefined") {
throw new ReferenceError('Promise not supported.');
}

return new Promise(function(resolve, reject){

// Add success and error handlers within the promise.

Aja.on.call(self, 'success', function(data){
console.log('success!');
resolve(data);
});

Aja.on.call(self, 'error', function(data){
console.log('error!');
reject(data);
});

Aja.go.call(self);
});
}
};

Expand Down