How to wait 'for loop next iteration' until FileReader.onload result obtain
How we can handle async methods in loops? I have a problem in an Angular program, I can't handle async methods. I want to wait for async methods.
is there any way to wait for async methods in for loop.
Here is my code:
msg: string = ;
filePicked() {
this.msg = ;
this.msg.push("file picked");
const file: File = new File([""], "C:UsersArun GirivasanDownloadsabout.jpg");
for (var i = 0; i < 10; i++) {
const reader = new FileReader();
reader.onload = () {
this.msg.push("file loaded successfully");
}
reader.readAsDataURL(file);
this.msg.push(i.toString());
}
html:
<div *ngFor="let m of msg">{{m}}</div>
output:
file picked
0
1
2
3
4
5
6
7
8
9
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
I want output like :
file picked
file loaded successfully
0
file loaded successfully
1
file loaded successfully
2
file loaded successfully
3
file loaded successfully
4
file loaded successfully
5
file loaded successfully
6
file loaded successfully
7
file loaded successfully
8
file loaded successfully
9
How can I do this?
angular typescript filereader
add a comment |
How we can handle async methods in loops? I have a problem in an Angular program, I can't handle async methods. I want to wait for async methods.
is there any way to wait for async methods in for loop.
Here is my code:
msg: string = ;
filePicked() {
this.msg = ;
this.msg.push("file picked");
const file: File = new File([""], "C:UsersArun GirivasanDownloadsabout.jpg");
for (var i = 0; i < 10; i++) {
const reader = new FileReader();
reader.onload = () {
this.msg.push("file loaded successfully");
}
reader.readAsDataURL(file);
this.msg.push(i.toString());
}
html:
<div *ngFor="let m of msg">{{m}}</div>
output:
file picked
0
1
2
3
4
5
6
7
8
9
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
I want output like :
file picked
file loaded successfully
0
file loaded successfully
1
file loaded successfully
2
file loaded successfully
3
file loaded successfully
4
file loaded successfully
5
file loaded successfully
6
file loaded successfully
7
file loaded successfully
8
file loaded successfully
9
How can I do this?
angular typescript filereader
3
Possible duplicate of Call An Asynchronous Javascript Function Synchronously
– artem
Nov 24 '18 at 16:41
add a comment |
How we can handle async methods in loops? I have a problem in an Angular program, I can't handle async methods. I want to wait for async methods.
is there any way to wait for async methods in for loop.
Here is my code:
msg: string = ;
filePicked() {
this.msg = ;
this.msg.push("file picked");
const file: File = new File([""], "C:UsersArun GirivasanDownloadsabout.jpg");
for (var i = 0; i < 10; i++) {
const reader = new FileReader();
reader.onload = () {
this.msg.push("file loaded successfully");
}
reader.readAsDataURL(file);
this.msg.push(i.toString());
}
html:
<div *ngFor="let m of msg">{{m}}</div>
output:
file picked
0
1
2
3
4
5
6
7
8
9
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
I want output like :
file picked
file loaded successfully
0
file loaded successfully
1
file loaded successfully
2
file loaded successfully
3
file loaded successfully
4
file loaded successfully
5
file loaded successfully
6
file loaded successfully
7
file loaded successfully
8
file loaded successfully
9
How can I do this?
angular typescript filereader
How we can handle async methods in loops? I have a problem in an Angular program, I can't handle async methods. I want to wait for async methods.
is there any way to wait for async methods in for loop.
Here is my code:
msg: string = ;
filePicked() {
this.msg = ;
this.msg.push("file picked");
const file: File = new File([""], "C:UsersArun GirivasanDownloadsabout.jpg");
for (var i = 0; i < 10; i++) {
const reader = new FileReader();
reader.onload = () {
this.msg.push("file loaded successfully");
}
reader.readAsDataURL(file);
this.msg.push(i.toString());
}
html:
<div *ngFor="let m of msg">{{m}}</div>
output:
file picked
0
1
2
3
4
5
6
7
8
9
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
file loaded successfully
I want output like :
file picked
file loaded successfully
0
file loaded successfully
1
file loaded successfully
2
file loaded successfully
3
file loaded successfully
4
file loaded successfully
5
file loaded successfully
6
file loaded successfully
7
file loaded successfully
8
file loaded successfully
9
How can I do this?
angular typescript filereader
angular typescript filereader
edited Nov 25 '18 at 7:20
Arun Girivasan
asked Nov 24 '18 at 16:30
Arun GirivasanArun Girivasan
155
155
3
Possible duplicate of Call An Asynchronous Javascript Function Synchronously
– artem
Nov 24 '18 at 16:41
add a comment |
3
Possible duplicate of Call An Asynchronous Javascript Function Synchronously
– artem
Nov 24 '18 at 16:41
3
3
Possible duplicate of Call An Asynchronous Javascript Function Synchronously
– artem
Nov 24 '18 at 16:41
Possible duplicate of Call An Asynchronous Javascript Function Synchronously
– artem
Nov 24 '18 at 16:41
add a comment |
2 Answers
2
active
oldest
votes
You'll need to extract the filereader code into another function and return a Promise or Observable.
private readFile(file: File): Observable<string> {
return new Observable(obs => {
const reader = new FileReader();
reader.onload = () => {
obs.next(reader.result as string);
obs.complete();
}
reader.readAsDataURL(file);
});
}
I prepared a quick example with Observable at https://stackblitz.com/edit/angular-pt3pbv
add a comment |
We can do it by looping the code with our own custom loop.
This work for me:
ngOnInit() {
this.filePicked();
}
filePicked() {
this.msg = ;
this.msg.push("file picked");
const file: File = new File(["abcd"], "C:UsersArun GirivasanDownloadsabout.jpg");
this.customLoop(0, 10, file);
}
customLoop(index: number, limit: number, file: File) {
this.readFileUrl(file, (msg: string) => {
this.msg.push(msg);
this.msg.push(index.toString());
if (index < limit - 1) {
this.customLoop(++index, limit, file);
}
});
}
readFileUrl(file: File, callback: (msg: string) => any) {
const reader = new FileReader();
reader.onload = (function (f) {
return function (e) {
callback("file loaded successfully");
}
})(file);
reader.readAsDataURL(file);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460161%2fhow-to-wait-for-loop-next-iteration-until-filereader-onload-result-obtain%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You'll need to extract the filereader code into another function and return a Promise or Observable.
private readFile(file: File): Observable<string> {
return new Observable(obs => {
const reader = new FileReader();
reader.onload = () => {
obs.next(reader.result as string);
obs.complete();
}
reader.readAsDataURL(file);
});
}
I prepared a quick example with Observable at https://stackblitz.com/edit/angular-pt3pbv
add a comment |
You'll need to extract the filereader code into another function and return a Promise or Observable.
private readFile(file: File): Observable<string> {
return new Observable(obs => {
const reader = new FileReader();
reader.onload = () => {
obs.next(reader.result as string);
obs.complete();
}
reader.readAsDataURL(file);
});
}
I prepared a quick example with Observable at https://stackblitz.com/edit/angular-pt3pbv
add a comment |
You'll need to extract the filereader code into another function and return a Promise or Observable.
private readFile(file: File): Observable<string> {
return new Observable(obs => {
const reader = new FileReader();
reader.onload = () => {
obs.next(reader.result as string);
obs.complete();
}
reader.readAsDataURL(file);
});
}
I prepared a quick example with Observable at https://stackblitz.com/edit/angular-pt3pbv
You'll need to extract the filereader code into another function and return a Promise or Observable.
private readFile(file: File): Observable<string> {
return new Observable(obs => {
const reader = new FileReader();
reader.onload = () => {
obs.next(reader.result as string);
obs.complete();
}
reader.readAsDataURL(file);
});
}
I prepared a quick example with Observable at https://stackblitz.com/edit/angular-pt3pbv
answered Nov 24 '18 at 17:00
A.WinnenA.Winnen
857119
857119
add a comment |
add a comment |
We can do it by looping the code with our own custom loop.
This work for me:
ngOnInit() {
this.filePicked();
}
filePicked() {
this.msg = ;
this.msg.push("file picked");
const file: File = new File(["abcd"], "C:UsersArun GirivasanDownloadsabout.jpg");
this.customLoop(0, 10, file);
}
customLoop(index: number, limit: number, file: File) {
this.readFileUrl(file, (msg: string) => {
this.msg.push(msg);
this.msg.push(index.toString());
if (index < limit - 1) {
this.customLoop(++index, limit, file);
}
});
}
readFileUrl(file: File, callback: (msg: string) => any) {
const reader = new FileReader();
reader.onload = (function (f) {
return function (e) {
callback("file loaded successfully");
}
})(file);
reader.readAsDataURL(file);
}
add a comment |
We can do it by looping the code with our own custom loop.
This work for me:
ngOnInit() {
this.filePicked();
}
filePicked() {
this.msg = ;
this.msg.push("file picked");
const file: File = new File(["abcd"], "C:UsersArun GirivasanDownloadsabout.jpg");
this.customLoop(0, 10, file);
}
customLoop(index: number, limit: number, file: File) {
this.readFileUrl(file, (msg: string) => {
this.msg.push(msg);
this.msg.push(index.toString());
if (index < limit - 1) {
this.customLoop(++index, limit, file);
}
});
}
readFileUrl(file: File, callback: (msg: string) => any) {
const reader = new FileReader();
reader.onload = (function (f) {
return function (e) {
callback("file loaded successfully");
}
})(file);
reader.readAsDataURL(file);
}
add a comment |
We can do it by looping the code with our own custom loop.
This work for me:
ngOnInit() {
this.filePicked();
}
filePicked() {
this.msg = ;
this.msg.push("file picked");
const file: File = new File(["abcd"], "C:UsersArun GirivasanDownloadsabout.jpg");
this.customLoop(0, 10, file);
}
customLoop(index: number, limit: number, file: File) {
this.readFileUrl(file, (msg: string) => {
this.msg.push(msg);
this.msg.push(index.toString());
if (index < limit - 1) {
this.customLoop(++index, limit, file);
}
});
}
readFileUrl(file: File, callback: (msg: string) => any) {
const reader = new FileReader();
reader.onload = (function (f) {
return function (e) {
callback("file loaded successfully");
}
})(file);
reader.readAsDataURL(file);
}
We can do it by looping the code with our own custom loop.
This work for me:
ngOnInit() {
this.filePicked();
}
filePicked() {
this.msg = ;
this.msg.push("file picked");
const file: File = new File(["abcd"], "C:UsersArun GirivasanDownloadsabout.jpg");
this.customLoop(0, 10, file);
}
customLoop(index: number, limit: number, file: File) {
this.readFileUrl(file, (msg: string) => {
this.msg.push(msg);
this.msg.push(index.toString());
if (index < limit - 1) {
this.customLoop(++index, limit, file);
}
});
}
readFileUrl(file: File, callback: (msg: string) => any) {
const reader = new FileReader();
reader.onload = (function (f) {
return function (e) {
callback("file loaded successfully");
}
})(file);
reader.readAsDataURL(file);
}
answered Dec 2 '18 at 3:48
Arun GirivasanArun Girivasan
155
155
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460161%2fhow-to-wait-for-loop-next-iteration-until-filereader-onload-result-obtain%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Possible duplicate of Call An Asynchronous Javascript Function Synchronously
– artem
Nov 24 '18 at 16:41