Book Image

Mastering JavaScript Promises

Book Image

Mastering JavaScript Promises

Overview of this book

Table of Contents (16 chapters)
Mastering JavaScript Promises
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Promise as a proxy


One marvelous thing about a promise that distinguishes it from the rest is that it can act as a proxy for another object, not only for local objects but also for a remote object. There are methods that let you confidently employ properties or call functions. All of these exchanges return promises, so that they can be chained.

Here is list of functions you can use as proxies of a promise:

Direct manipulation

Using a promise as a proxy

value.foo

promise.get("foo")

value.foo = value

promise.put("foo", value)

delete value.foo

promise.del("foo")

value.foo(...args)

promise.post("foo", [args])

value.foo(...args)

promise.invoke("foo", ...args)

value(...args)

promise.fapply([args])

value(...args)

promise.fcall(...args)

You can trim round-trips by using these functions instead of then() if the promise is a proxy for a remote object.

Even in the case of local objects, these methods can be used as shorthand for particularly-simple gratification...