Am facing a error with the Following code that the Async action "FILE_UPLOAD_SUCCESS" emits even before the promise is resolved so the action payload returns undefined. Any idea on how to resolve it???
Action Creator
export const upload = (file) => {
return (dispatch)=>{
dispatch({type:"FILE_UPLOAD_START"})
return new Promise.resolve(()=>{
uploadDoc(file).then((hash)=>dispatch({type:"FILE_UPLOAD_SUCCESS",payload:hash}))
}).catch((err)=>{
dispatch({type:"FILE_UPLOAD_ERROR",payload:err})
})
}
}
uploadDoc
export const uploadDoc = (file) =>{
return new Promise((resolve,reject)=>{
var reader = new FileReader();
reader.readAsArrayBuffer(file[0]);
reader.onloadend = (evt) =>{
resolve(reader);
}
}).then((reader)=>{
return Promise.resolve(()=>{
const buffer = Buffer.from(reader.result);
ipfs.add(buffer)
.then((res)=>{
return res[0].hash;
})
})
})
}