Promise returning in Angular [duplicate]
up vote
1
down vote
favorite
This question already has an answer here:
What is the explicit promise construction antipattern and how do I avoid it?
2 answers
I'm new to Angular so it's still rather difficult for me to write code in an efficient way. I've recently come upon this problem regarding Promise chaining and I'm wondering if there is a better way to go at it.
The gist is like this: I have function A, which needs result from function B. If I get a certain result from B, Then I would call C. B and C both returns Promises.
So in the end I got a very ugly block of code like this.
funcA(): Promise<MyObj> {
return new Promise(function(resolve, reject) {
funcB().then (res => {
check if res satisfies condition
funcC().then( obj => {
process obj
resolve(obj)
}, reason => doSomething then reject)
}, reason => doSomething then reject)
}
}
The code works and serves my need, but it's difficult to read and I think there might be much better way to do this. The problem gets worse since there are a lot of places that I need to make similar call and sometimes the chaining might be even more complicated. So if anyone can provide a suggestion of how to better reformat my code or to how to rewrite function with Promise chaining I would greatly appreciate it.
angular typescript promise angular-promise
marked as duplicate by trincot, Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
1
down vote
favorite
This question already has an answer here:
What is the explicit promise construction antipattern and how do I avoid it?
2 answers
I'm new to Angular so it's still rather difficult for me to write code in an efficient way. I've recently come upon this problem regarding Promise chaining and I'm wondering if there is a better way to go at it.
The gist is like this: I have function A, which needs result from function B. If I get a certain result from B, Then I would call C. B and C both returns Promises.
So in the end I got a very ugly block of code like this.
funcA(): Promise<MyObj> {
return new Promise(function(resolve, reject) {
funcB().then (res => {
check if res satisfies condition
funcC().then( obj => {
process obj
resolve(obj)
}, reason => doSomething then reject)
}, reason => doSomething then reject)
}
}
The code works and serves my need, but it's difficult to read and I think there might be much better way to do this. The problem gets worse since there are a lot of places that I need to make similar call and sometimes the chaining might be even more complicated. So if anyone can provide a suggestion of how to better reformat my code or to how to rewrite function with Promise chaining I would greatly appreciate it.
angular typescript promise angular-promise
marked as duplicate by trincot, Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
See duplicate reference. In short: you should not neednew Promise
whenfuncB()
already returns one. Just return it or whatever you chain to it withthen
.
– trincot
2 days ago
Thanks. This is exactly what I've been looking for.
– Sylph
2 days ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This question already has an answer here:
What is the explicit promise construction antipattern and how do I avoid it?
2 answers
I'm new to Angular so it's still rather difficult for me to write code in an efficient way. I've recently come upon this problem regarding Promise chaining and I'm wondering if there is a better way to go at it.
The gist is like this: I have function A, which needs result from function B. If I get a certain result from B, Then I would call C. B and C both returns Promises.
So in the end I got a very ugly block of code like this.
funcA(): Promise<MyObj> {
return new Promise(function(resolve, reject) {
funcB().then (res => {
check if res satisfies condition
funcC().then( obj => {
process obj
resolve(obj)
}, reason => doSomething then reject)
}, reason => doSomething then reject)
}
}
The code works and serves my need, but it's difficult to read and I think there might be much better way to do this. The problem gets worse since there are a lot of places that I need to make similar call and sometimes the chaining might be even more complicated. So if anyone can provide a suggestion of how to better reformat my code or to how to rewrite function with Promise chaining I would greatly appreciate it.
angular typescript promise angular-promise
This question already has an answer here:
What is the explicit promise construction antipattern and how do I avoid it?
2 answers
I'm new to Angular so it's still rather difficult for me to write code in an efficient way. I've recently come upon this problem regarding Promise chaining and I'm wondering if there is a better way to go at it.
The gist is like this: I have function A, which needs result from function B. If I get a certain result from B, Then I would call C. B and C both returns Promises.
So in the end I got a very ugly block of code like this.
funcA(): Promise<MyObj> {
return new Promise(function(resolve, reject) {
funcB().then (res => {
check if res satisfies condition
funcC().then( obj => {
process obj
resolve(obj)
}, reason => doSomething then reject)
}, reason => doSomething then reject)
}
}
The code works and serves my need, but it's difficult to read and I think there might be much better way to do this. The problem gets worse since there are a lot of places that I need to make similar call and sometimes the chaining might be even more complicated. So if anyone can provide a suggestion of how to better reformat my code or to how to rewrite function with Promise chaining I would greatly appreciate it.
This question already has an answer here:
What is the explicit promise construction antipattern and how do I avoid it?
2 answers
angular typescript promise angular-promise
angular typescript promise angular-promise
edited 2 days ago


trichetriche
23.6k41949
23.6k41949
asked 2 days ago
Sylph
408
408
marked as duplicate by trincot, Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by trincot, Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
See duplicate reference. In short: you should not neednew Promise
whenfuncB()
already returns one. Just return it or whatever you chain to it withthen
.
– trincot
2 days ago
Thanks. This is exactly what I've been looking for.
– Sylph
2 days ago
add a comment |
See duplicate reference. In short: you should not neednew Promise
whenfuncB()
already returns one. Just return it or whatever you chain to it withthen
.
– trincot
2 days ago
Thanks. This is exactly what I've been looking for.
– Sylph
2 days ago
See duplicate reference. In short: you should not need
new Promise
when funcB()
already returns one. Just return it or whatever you chain to it with then
.– trincot
2 days ago
See duplicate reference. In short: you should not need
new Promise
when funcB()
already returns one. Just return it or whatever you chain to it with then
.– trincot
2 days ago
Thanks. This is exactly what I've been looking for.
– Sylph
2 days ago
Thanks. This is exactly what I've been looking for.
– Sylph
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
Since you're dealing with promises here, you can use the async
await
syntax for a cleaner implementation. Something along the lines of this:
async funcA() {
try {
const res = await funcB();
if (check your condition with res) {
const obj = await funcC();
const resolvedObj = resolve(obj); // your custom implementation here;
return resoalvedObj;
}
} catch (error) {
console.log(`Something went wrong. Here's the error: ${JSON.stringify(error)}`);
}
}
NOTE: This might not be the exact implementation that you should go for. But this definitely is a good starting point for you to clean your implementation.
add a comment |
up vote
0
down vote
You could use Observables (here is an old answer of mine as to why you should), but since you asked for promises, let me deliver an answer.
The best way to deal with this is to simply chain them altogether. As long as your then
returns something, you can make it easy, as shown here.
This would give :
funcA(): Promise<MyObj> {
return promiseB()
.then(res => promiseC(res))
...
.then(res => promiseZ(res))
);
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Since you're dealing with promises here, you can use the async
await
syntax for a cleaner implementation. Something along the lines of this:
async funcA() {
try {
const res = await funcB();
if (check your condition with res) {
const obj = await funcC();
const resolvedObj = resolve(obj); // your custom implementation here;
return resoalvedObj;
}
} catch (error) {
console.log(`Something went wrong. Here's the error: ${JSON.stringify(error)}`);
}
}
NOTE: This might not be the exact implementation that you should go for. But this definitely is a good starting point for you to clean your implementation.
add a comment |
up vote
1
down vote
Since you're dealing with promises here, you can use the async
await
syntax for a cleaner implementation. Something along the lines of this:
async funcA() {
try {
const res = await funcB();
if (check your condition with res) {
const obj = await funcC();
const resolvedObj = resolve(obj); // your custom implementation here;
return resoalvedObj;
}
} catch (error) {
console.log(`Something went wrong. Here's the error: ${JSON.stringify(error)}`);
}
}
NOTE: This might not be the exact implementation that you should go for. But this definitely is a good starting point for you to clean your implementation.
add a comment |
up vote
1
down vote
up vote
1
down vote
Since you're dealing with promises here, you can use the async
await
syntax for a cleaner implementation. Something along the lines of this:
async funcA() {
try {
const res = await funcB();
if (check your condition with res) {
const obj = await funcC();
const resolvedObj = resolve(obj); // your custom implementation here;
return resoalvedObj;
}
} catch (error) {
console.log(`Something went wrong. Here's the error: ${JSON.stringify(error)}`);
}
}
NOTE: This might not be the exact implementation that you should go for. But this definitely is a good starting point for you to clean your implementation.
Since you're dealing with promises here, you can use the async
await
syntax for a cleaner implementation. Something along the lines of this:
async funcA() {
try {
const res = await funcB();
if (check your condition with res) {
const obj = await funcC();
const resolvedObj = resolve(obj); // your custom implementation here;
return resoalvedObj;
}
} catch (error) {
console.log(`Something went wrong. Here's the error: ${JSON.stringify(error)}`);
}
}
NOTE: This might not be the exact implementation that you should go for. But this definitely is a good starting point for you to clean your implementation.
answered 2 days ago
SiddAjmera
10.1k21137
10.1k21137
add a comment |
add a comment |
up vote
0
down vote
You could use Observables (here is an old answer of mine as to why you should), but since you asked for promises, let me deliver an answer.
The best way to deal with this is to simply chain them altogether. As long as your then
returns something, you can make it easy, as shown here.
This would give :
funcA(): Promise<MyObj> {
return promiseB()
.then(res => promiseC(res))
...
.then(res => promiseZ(res))
);
}
add a comment |
up vote
0
down vote
You could use Observables (here is an old answer of mine as to why you should), but since you asked for promises, let me deliver an answer.
The best way to deal with this is to simply chain them altogether. As long as your then
returns something, you can make it easy, as shown here.
This would give :
funcA(): Promise<MyObj> {
return promiseB()
.then(res => promiseC(res))
...
.then(res => promiseZ(res))
);
}
add a comment |
up vote
0
down vote
up vote
0
down vote
You could use Observables (here is an old answer of mine as to why you should), but since you asked for promises, let me deliver an answer.
The best way to deal with this is to simply chain them altogether. As long as your then
returns something, you can make it easy, as shown here.
This would give :
funcA(): Promise<MyObj> {
return promiseB()
.then(res => promiseC(res))
...
.then(res => promiseZ(res))
);
}
You could use Observables (here is an old answer of mine as to why you should), but since you asked for promises, let me deliver an answer.
The best way to deal with this is to simply chain them altogether. As long as your then
returns something, you can make it easy, as shown here.
This would give :
funcA(): Promise<MyObj> {
return promiseB()
.then(res => promiseC(res))
...
.then(res => promiseZ(res))
);
}
answered 2 days ago


trichetriche
23.6k41949
23.6k41949
add a comment |
add a comment |
See duplicate reference. In short: you should not need
new Promise
whenfuncB()
already returns one. Just return it or whatever you chain to it withthen
.– trincot
2 days ago
Thanks. This is exactly what I've been looking for.
– Sylph
2 days ago