Skip to content

Instantly share code, notes, and snippets.

@SergeiGolos
Last active August 29, 2015 14:07
Show Gist options
  • Save SergeiGolos/f12ec6812fe93e1ef21a to your computer and use it in GitHub Desktop.
Save SergeiGolos/f12ec6812fe93e1ef21a to your computer and use it in GitHub Desktop.
'use strict';
/**
* @ngdoc service
* @name toPromiseApp.toPromise
* @description
* # toPromise
* # a service call for wrapping synchronous calls to result in a promise.
*/
angular.module('toPromiseApp')
.factory('toPromise', [
'$q',
'$timeout',
function ($q, $timeout) {
return function(arg) {
var fn = arg;
return function() {
var result = $q.defer();
var applyArgs = arguments;
$timeout(function() {
result.resolve(Function.prototype.apply(fn, applyArgs));
}, 0);
return result.promise;
}
};
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment