我使用Angular试图将一个大型函数分离为多个较小的函数。使用以下内容:
from M2Crypto import RSA, X509
x509 = X509.load_cert("your_cert.pem")
rsa = x509.get_pubkey().get_rsa()
# building a secret as a 32bytes bytearray
my_secret = "This is my secret, and nobody knows it"
my_secret_bin = bytearray(my_secret)
# this should be exactly 32
len(my_secret_bin)
import base64
import hashlib
my_hash = base64.b64encode((hashlib.sha256(my_secret_bin[0:32])).digest())
my_encrypted_secret = base64.b64encode(rsa.public_encrypt(my_secret_bin[0:32], RSA.pkcs1_oaep_padding))
# This can be used to call it via API
# note that the 'your_cert' certificate name should match
upload_params = { 'SecretValue':my_encrypted_secret, 'SecretValueHash':my_hash, 'SecretValueCertificate':'your_cert'}
# write to files
with open('encrypted_secret.bb64', 'wb') as f:
f.write(my_encrypted_secret)
with open('secret_hash.b64', 'wb') as f:
f.write(my_hash)
但是,在getSites() {
this.http.get(`https://example.com/dev/_api/web/lists/getbytitle('Site Master')/items?$select=*,Hub/Title&$expand=Hub&$filter=Hub/Title eq 'Project Hub' and Active eq 'Yes'`).subscribe(data => {
console.log(`project sites`, data['value'])
this.sites = data['value']
this.getLists() //Wait for this.sites to populate, then fire
})
}
getLists() {
this.sites.forEach(site => {
this.http.get(`${site['Site']}/_api/web/lists/?$select=*&$filter=BaseTemplate eq 171`).subscribe(data => {
console.log("Site's lists", data['value'])
this.getTasks(data['value'], site)
})
})
}
可以从第一个请求中完全填充之前,getLists()
正在触发。我该怎么解决?
答案 0 :(得分:1)
您可以使用concatMap管道(这将等待getSites完成),而不是同时订阅这两个功能
getSites():Observable<any>{
this.http(...).pipe(map(() => ....)
}
getList(): Observable<any>{
// do some stuff
}
// and use it like this
getSites().pipe(contactMap((getSiteResult) => getList(getSiteResult)).subscribe()
希望有帮助