|
|
|
|
|
by Prefinem
2973 days ago
|
|
Life is more fun with fat arrows sometimes new Promise(function (resolve, reject) {
methodOne(data, function (error, response) {
if (erorr) {
reject(error);
} else {
resolve(response);
}
})
})
vs new Promise((resolve, reject) => methodOne(data, (error, response) => error ? reject(error) : resolve(response)));
|
|
In your example, the code with arrow functions is smaller, but it is not more readable. Because there is no indentation, it is difficult to understand how code is nested. I cannot read that.
It can be rewritten using `deferred` pattern:
This way we can get rid of a callback in the Promise constructor. Please note that our code now looks sequential and we clearly see what happens after what. Asynchronous code is difficult to write and read; therefore we must put an extra effort to make it easier.In my opinion it is generally bad idea to nest more that 1-2 levels of functions inside each other.