在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:kriskowal/q开源软件地址:https://github.com/kriskowal/q开源编程语言:JavaScript 97.1%开源软件介绍:NotePlease consider using JavaScript promises instead of Q. Native promises are faster, have better tooling support and are the future. When work on Q began, promises were an academic novelty in JavaScript, unlikely to be adopted much less popular, though obviously full of…promise. Callbacks dominated the landscape. Q aimed to introduce a technology to JavaScript that had been proven and vetted in languages like E and C♯. With four years of incubation, evangelism, education, and feedback, promises became part of the language. Every modern browser contains a built-in Q isn't going anywhere. The code is still here and bugs will be fixed but further development has been unnecessary for many years. We encourage you to read the code and the explainers to glimpse into the history of the internet. QIf a function cannot return a value or throw an exception without blocking, it can return a promise instead. A promise is an object that represents the return value or the thrown exception that the function may eventually provide. A promise can also be used as a proxy for a remote object to overcome latency. On the first pass, promises can mitigate the “Pyramid of Doom”: the situation where code marches to the right faster than it marches forward. step1(function (value1) {
step2(value1, function(value2) {
step3(value2, function(value3) {
step4(value3, function(value4) {
// Do something with value4
});
});
});
}); With a promise library, you can flatten the pyramid. Q.fcall(promisedStep1)
.then(promisedStep2)
.then(promisedStep3)
.then(promisedStep4)
.then(function (value4) {
// Do something with value4
})
.catch(function (error) {
// Handle any error from all above steps
})
.done(); With this approach, you also get implicit error propagation, just like The callback approach is called an “inversion of control”. A function that accepts a callback instead of a return value is saying, “Don’t call me, I’ll call you.”. Promises un-invert the inversion, cleanly separating the input arguments from control flow arguments. This simplifies the use and creation of API’s, particularly variadic, rest and spread arguments. Getting StartedThe Q module can be loaded as:
Q can exchange promises with jQuery, Dojo, When.js, WinJS, and more. ResourcesOur wiki contains a number of useful resources, including:
We'd also love to have you join the Q-Continuum mailing list. TutorialPromises have a promiseMeSomething()
.then(function (value) {
}, function (reason) {
}); If Note that resolution of a promise is always asynchronous: that is, the
fulfillment or rejection handler will always be called in the next turn of the
event loop (i.e. In this tutorial, we begin with how to consume and work with promises. We'll
talk about how to create them, and thus create functions like
PropagationThe var outputPromise = getInputPromise()
.then(function (input) {
}, function (reason) {
}); The
If the var outputPromise = getInputPromise()
.then(function (value) {
}); If the input promise gets fulfilled and you omit the fulfillment handler, the
value will go to var outputPromise = getInputPromise()
.then(null, function (error) {
}); Q promises provide a var outputPromise = getInputPromise()
.fail(function (error) {
}); If you are writing JavaScript for modern engines only or using
CoffeeScript, you may use Promises also have a var outputPromise = getInputPromise()
.fin(function () {
// close files, database connections, stop servers, conclude tests
});
If you are writing JavaScript for modern engines only or using
CoffeeScript, you may use ChainingThere are two ways to chain promises. You can chain promises either inside or outside handlers. The next two examples are equivalent. return getUsername()
.then(function (username) {
return getUser(username)
.then(function (user) {
// if we get here without an error,
// the value returned here
// or the exception thrown here
// resolves the promise returned
// by the first line
})
}); return getUsername()
.then(function (username) {
return getUser(username);
})
.then(function (user) {
// if we get here without an error,
// the value returned here
// or the exception thrown here
// resolves the promise returned
// by the first line
}); The only difference is nesting. It’s useful to nest handlers if you need to capture multiple input values in your closure. function authenticate() {
return getUsername()
.then(function (username) {
return getUser(username);
})
// chained because we will not need the user name in the next event
.then(function (user) {
return getPassword()
// nested because we need both user and password next
.then(function (password) {
if (user.passwordHash !== hash(password)) {
throw new Error("Can't authenticate");
}
});
});
} CombinationYou can turn an array of promises into a promise for the whole,
fulfilled array using return Q.all([
eventualAdd(2, 2),
eventualAdd(10, 20)
]); If you have a promise for an array, you can use function eventualAdd(a, b) {
return Q.spread([a, b], function (a, b) {
return a + b;
})
} But return getUsername()
.then(function (username) {
return [username, getUser(username)];
})
.spread(function (username, user) {
}); The Q.allSettled(promises)
.then(function (results) {
results.forEach(function (result) {
if (result.state === "fulfilled") {
var value = result.value;
} else {
var reason = result.reason;
}
});
}); The Q.any(promises)
.then(function (first) {
// Any of the promises was fulfilled.
}, function (error) {
// All of the promises were rejected.
}); SequencesIf you have a number of promise-producing functions that need to be run sequentially, you can of course do so manually: return foo(initialVal).then(bar).then(baz).then(qux); However, if you want to run a dynamically constructed sequence of functions, you'll want something like this: var funcs = [foo, bar, baz, qux];
var result = Q(initialVal);
funcs.forEach(function (f) {
result = result.then(f);
});
return result; You can make this slightly more compact using return funcs.reduce(function (soFar, f) {
return soFar.then(f);
}, Q(initialVal)); Or, you could use the ultra-compact version: return funcs.reduce(Q.when, Q(initialVal)); Handling ErrorsOne sometimes-unintuitive aspect of promises is that if you throw an exception in the fulfillment handler, it will not be caught by the error handler. return foo()
.then(function (value) {
throw new Error("Can't bar.");
}, function (error) {
// We only get here if "foo" fails
}); To see why this is, consider the parallel between promises and
In terms of promises, this means chaining your rejection handler: return foo()
.then(function (value) {
throw new Error("Can't bar.");
})
.fail(function (error) {
// We get here with either foo's error or bar's error
}); Progress NotificationIt's possible for promises to report their progress, e.g. for tasks that take a
long time like a file upload. Not all promises will implement progress
notifications, but for those that do, you can consume the progress values using
a third parameter to return uploadFile()
.then(function () {
// Success uploading the file
}, function (err) {
// There was an error, and we get the reason for error
}, function (progress) {
// We get notified of the upload's progress as it is executed
}); Like return uploadFile().progress(function (progress) {
// We get notified of the upload's progress
}); The EndWhen you get to the end of a chain of promises, you should either return the last promise or end the chain. Since handlers catch errors, it’s an unfortunate pattern that the exceptions can go unobserved. So, either return it, return foo()
.then(function () {
return "bar";
}); Or, end it. foo()
.then(function () {
return "bar";
})
.done(); Ending a promise chain makes sure that, if an error doesn’t get handled before the end, it will get rethrown and reported. This is a stopgap. We are exploring ways to make unhandled errors visible without any explicit handling. The BeginningEverything above assumes you get a promise from somewhere else. This is the common case. Every once in a while, you will need to create a promise from scratch.
Using |
请发表评论