filereader.js icon indicating copy to clipboard operation
filereader.js copied to clipboard

SetSync don't work

Open miltonmagalhaes opened this issue 10 years ago • 1 comments

The code run like Step 1 -> Step 3 -> Step 2

I need to know the TypeArq content after reading the file, ie synchronous manner. However, the contents of TypeArq return anything.

The Code: var TypeArq = "" alert("Step 1"); FileReaderJS.setSync(true); FileReaderJS.setupInput(document.getElementById('input'), { readAsDefault: "Text", on: { load: function(e, file) { TypeArq = e.target.result.substring(1,8); console.log("Funcionou--->" + e.target.result.substring(1,8)); alert("Step 2"); } }
}); alert("Step 3"); alert(TypeArq)

miltonmagalhaes avatar Jan 12 '15 12:01 miltonmagalhaes

It's synchronous once the user begins the input (not on initialization). So there is an async gap between the code that sets up the reader and the user actually selecting a file. You can read more about this FileReaderSync object on MDN.

In your case, you could move 'step 3' and any logic within it into the load event and things would execute in the order you want.

bgrins avatar Jan 12 '15 16:01 bgrins