私は約束の連鎖が必要な要件があります。私のイオニアアプリケーションでは、ファイルのリストを繰り返し処理し、それらを圧縮する必要があります。その後、ジップはデバイス自体(この場合はiPhone)に保存する必要があります。Promise.allを含む複数の約束を結ぶ
私はすでに配列に圧縮する必要があるファイルのリストを持っています。そこで、私はそれらを繰り返し、$ cordovaFileを使ってこれらのファイルのbinay内容を取得しています。次にJSZipオブジェクトにバイナリを追加します。 zipファイルを生成できるように、すべてのファイルのバイナリコンテンツをzip.fileに追加する必要があります。
//wrapping in Promise.all so that we don't proceed until we have the content of all files added to zip
var zip = new JSZip();
return Promise.all(
filesList.forEach(function(file) {
console.log('file to be added using $cordovaFile '+file);
// Getting the content of each file using $cordovaFile. This returns a promise.
return $cordovaFile.readAsBinaryString(cordova.file.dataDirectory + $rootScope.username, file)
.then(function(binaryData) {
return new Promise(function(resolve, reject) {
//Adding content of all files to zip.file so that it can be zipped in the next step.
resolve(zip.file(file, binaryData, {binary: true}));
})
})
.catch(function(error) {
console.log('Error during fetch content or zipping '+JSON.stringify(error));
})
})
)
zip.fileにすべての内容が含まれていると、私はzipを生成するJSZipの別の関数を呼び出しています。これも約束を返すので、私は$ cordovaFile.writeFileにチェーンして、zipをローカルに書くことができるようにする必要があります。 $ cordovaFile.writeFileはまた、チェーン内の最後の約束であるPromiseを返します。
.then(function(zipData) {
// async request to generate the zip
return zipData.generateAsync({type:"blob"});
}).then(function (blob) {
// once we have the zip, save it to the device
$cordovaFile.writeFile(cordova.file.dataDirectory+$rootScope.username, 'abc.zip', blob, true)
.then(function(data) {
console.log('Zip file written to device at '+cordova.file.dataDirectory+$rootScope.username);
})
}).catch(function(error) {
console.log('Error while zipping and writing '+JSON.stringify(error));
})
これは
var zipFiles = function(filesList) {
var zip = new JSZip();
return Promise.all(
filesList.forEach(function(file) {
return $cordovaFile.readAsBinaryString(cordova.file.dataDirectory + $rootScope.username, file)
.then(function(binaryData) {
return new Promise(function(resolve, reject) {
resolve(zip.file(file, binaryData, {binary: true}));
})
})
.catch(function(error) {
console.log('Error during fetch content or zipping '+JSON.stringify(error));
})
})
)
.then(function(zipData) {
return zipData.generateAsync({type:"blob"});
}).then(function (blob) {
$cordovaFile.writeFile(cordova.file.dataDirectory+$rootScope.username, 'abc.zip', blob, true)
.then(function(data) {
console.log('Zip file written to device at '+cordova.file.dataDirectory+$rootScope.username);
})
}).catch(function(error) {
console.log('Error while zipping and writing '+JSON.stringify(error));
})
}
チャレンジPromise.allが完了した後、何も実行されないされることをあるように、完全なコードがどのように見えるかです。だから、何も開始「を(関数(zipData)」は実行されます。
を私は感じて、それは私が約束を連鎖しています方法とは何かを持っている。すべてのヘルプは高く評価されます。
私が気づいていなかった私のアプローチでは、質問に答えるだけでなく、他の間違いを修正するために、Georgi-itに感謝します。 – kayasa
喜んで私は@ kayasaを助けることができました –