Quick question, I create this File
object, write content to disk using it, and then return it from inside an async function like so:
...
File file = await makeFileObject(filename);
return await file.writeAsString(content);
// fyi writeAsString returns Future<File>
but I then I refactored it using the ..
notation, and it says it doesn't need the await
after return
That's weird. Does ..
await the write?
Am I returning the File
object before the write occurs or the File
object returned by writeAsString
?
...
return (await makeFileObject(filename))
..writeAsString(content);
Is the refactor functionally equivalent to the original?
CodePudding user response:
No, the refactor is not equivalent to the original. x..y
evaluates to x
and discards whatever y
evaluates to. In your case, that means that you discard the Future
returned by writeAsString
, and it therefore would not be await
ed. If you want your function to return only after writeAsString
completes, you cannot use it with ..
.
If you want to avoid having the file
variable, you would need to do:
return await (await makeFileObject(filename)).writeAsString(content);
However, I think that the original is more readable, and I would stick with that.